1

I'm trying to generate arrays and calculate there values depending on some function. and I want to save each generated array into array List PQ.

the important methods are : init: to initiate a series of arrays calculate: is to calculate array measurement or value in this method I want to check if this array is already have been calculated by searching in PQ array which will have all previous calculated array.

badly, after each for stage for (j=0;j<s;j++) the sol[] object some how changed in the array list and the array list never updated with new values.

it is like there is object link between PQ.add(sol) and the calculate(solution);

how to remove this link i.e. pass-by-reference and convert it to pass-by-value so I can add new arrays to PQ Arraylist.

in another way how to pass array as value instead of reference ? this is my code:

ArrayList previous_values=new ArrayList();
ArrayList PQ=new ArrayList();

        void init(int index) 
            {
               int j;
               for (j=0;j<s;j++)
                    {
                    r = j+1;
                    array [index][j]=r*index;
                    solution[j]=array[index][j];
                    }
                f[index]=calculate(solution);}


        double calculate(int sol[]) 
        {


            double r;
            r=search_Previous(sol);
         if(r==-1)  {


             PQ.add(sol);
    r=sol[0]*5;
previous_value.add(r);
    }
        }


        public double search_Previous(int[] arr)
            {
                double d=-1;
                for(int i=0;i<PQ.size();i++)
                {
                    if(equal_arr(arr,(int[])(PQ.get(i))))
                    {
                     return (double)previous_value.get(i) ;  
                    }
                }
                return d;
            }

        public static boolean equal_arr(int[] list1, int[] list2) {



              // Now test if every element is the same
              for (int i = 0; i < list1.length; i++) {
                  if (list1[i] != list2[i])
                      return false; // If one is wrong then they all are wrong.
              }

              // If all these tests worked, then they are identical.
              return true;
        }

thanks

2
  • @Abreal.. Your question is not very clear.. Can you explain the flow of your problem, and what you are trying to do?? What is your PQ?? Commented Oct 7, 2012 at 9:52
  • "how to pass array as value instead of reference" First of all, everything in Java is passed by value. Always. You cannot "pass" an array, or any other object. You can only pass a reference to an object. And it is passed by value Commented Oct 8, 2012 at 8:45

2 Answers 2

2

Hope i get you question

.. how to pass array as value instead of reference ?

You need to copy the array. Use System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

Sign up to request clarification or add additional context in comments.

Comments

0
double calculate(int sol[]) 
{


    double r;
    r=search_Previous_f(sol);
 if(r==-1)  {


     PQ.add(sol);
}

btw you didn't closed the if statement

6 Comments

ok, I closed the if but my main problem is how to pass array as value instead of reference ?
what do you mean? after the run I want to have some thing like this in the PQ array list (23,3,5,6),(5,7,88,9), etc but I have only one result in PQ array list (55,6,7,8) which I think because of pass by reference? any clue about this?
@Abreal.. Everything in Java is passed by value.. So, what actually you mean by first comment??
somehow there is link or pointer between sol[] and solution array which lead to that always have a changeable result in the PQ(only one array at output) instead of adding new array to it ? hope it's clear now thanks if it's not because pass by reference then how to solve it any way
@Abreal Your method name do not match.. You are calling calculateFunction, whereas you have calculate method..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.