0

I am beginner to java coding, I am facing some complexity in updating the array.

My problem is I have an array named s1[] which consists of 10 elements from that I have to choose 1st 3 elements and I have to store in one array i.e. max1[] array and in next array I have to choose randomly 3 elements from the same array i.e. s1[] and store it in max6[] and I need to do comparison between the two arrays i.e. max1[] and max6[] so that the matching values will be stored.

My code looks like this (here f1 = 3)

        for (i1 = 0; i1 < f1; i1++) {
            max1[i1] = s1[i1];
            System.out.println("1st tree random leaf nodes   of phy m/c 1  at tree 1 :  " + max1[i1]);
            System.out.println(" \n ");
            max6[i1] = s1[(int) (Math.random() * f1)];
            System.out.println(" random leaf nodes  of  phy m/c 1  at tree 2: " + max6[i1]);
            System.out.println(" \n ");

        }

        for (int l1 = 0; l1 < f1; l1++) {
            for (int k1 = 0; k1 < f1; k1++) {
                if (max1[l1] == max6[k1]) {
                    c1[l1] = max1[l1];
                    k1++;
                    System.out.println("the value after crossover is:--------->" + c1[l1]);
                    break;
                }
            }
        }

now what I want is that I have to repeat the loop for some iterations so that in the next iteration the values of the max1 array in 1st iteration shouldn't be repeated means I have to permanently remove the elements from s1 array until s1 array contains zero elements in it

user2772831
  • 31
  • 1
  • 3

1 Answers1

0

I have two suggestions for you, it's up to you which you prefer.

Firstly I would suggest converting the array to a list and removing the necessary elements from the list then converting back to the array. Code follows :

List<Double> list = new ArrayList<Double>(Arrays.asList(s1));
list.remove(i1);
s1 = list.toArray(new Double[0]);

Secondly you can shift the specific element of the array to the back and just iterate one position less than the length. This will require you adding an extra int variable to either record the number of elements in the array or to record the amount of deleted elements, the former being the better idea. Code follows :

double temp;
for(int i = 0; i < test.length - 1; i++){
    temp = s1[i];
    s1[i] = s1[i + 1];
    s1[i + 1] = temp;
}
nrE = nrE - 1;

This will require you to have an int nrE with value of the length of the array(here 10) before your first for loop.

Both of these should be implemented inside the for loop at the end of the iteration. From what I've seen my first suggestion is used most.

Note : I may misunderstand what you are trying to achieve, but I believe your code for getting the 3 random elements is faulty. If f1=3 and you take element at the position Math.random() * f1 you will always get an element in the range 0 to 3. To fix this you can instead use Math.random() * s1.length

Vinc
  • 695
  • 2
  • 10
  • 26
  • thanks for your answer but i am not good in java collections and the array s1 is of double type can you please elaborate the second method with an example can you please tell me how to select 3 elements randomly from an array if i was wrong – user2772831 Mar 18 '14 at 14:14
  • Edited in code example aswell as the method to get random element. Also updated the first suggestion with appropriate code for double array though as you can see that will require you to instead have an array of the Double object and not the primitive double. You can ofcourse convert from a double array to List and back, but that seems to me like it might be too much hassle in this case. – Vinc Mar 19 '14 at 06:22
  • Thank you i will try it on your first suggestion.. – user2772831 Mar 19 '14 at 12:29