Skip to Main Content

How to combine arrays?

User_X0INSSep 19 2021

Hello,
I need to output any common elements that are in two arrays I instantiated in my array that randomly range from anywhere between 0 to 100, along with needing to output the common elements that will be deleted. While I previously compiled my program successfully until outputting the combination of the two arrays discussed above, I do not know how to incorporate my needed range or remove the common elements and create the output of these deleted elements. I also had the error "bad operand types for binary operator +" when attempting to print. Here is the code for my program:
/*
*I need to incorporate the requirements above into the following code
*/
//These classes allow for instantiating arrays and randomizing numbers
import java.util.Arrays;
import java.util.Random;

public class ArrRanProgress {
//This method declaration will carry out a Selection Sort Algorithm for necessary swapping
public static int[] SelectionSort(int[] array){

for(int i = 0; i \< array.length; i++){  
  int index = i;  
     
  //The smaller element swapped will be in its correct place  
  for (int j = i + 1; j \< array.length; j++){  
    if (array\[j\] \< array\[index\]){  
      index = j;  
    }  
  }  
     
  int leastValue = array\[index\];  
  array\[index\] = array\[i\];  
  array\[i\] = leastValue;  
}  
return array;  

}

public static void main(String args[]) {

//Random keyword will allow for choosing random values
Random generate = new Random();

//Declaring ListA and ListB arrays that allocate 50 integers each in memory
int[] ListA = new int[50];
int[] ListB = new int[50];

//for loops will initialize random values
for (int i = 0; i < ListA.length; i++){
ListA[i] = generate.nextInt();
}
System.out.println(Arrays.toString(ListA));
System.out.println("");

for (int i = 0; i < ListB.length; i++){
ListB[i] = generate.nextInt();
}
System.out.println(Arrays.toString(ListB));
System.out.println("");

//New arrays will be instantiated from old arrays in order swap elements into order
int[] OrderedListA = SelectionSort(ListA);
int[] OrderedListB = SelectionSort(ListB);

System.out.println(Arrays.toString(OrderedListA) + "\n");
System.out.println(Arrays.toString(OrderedListB) + "\n");

//Need assistance to combine the two ordered arrays to make the TheList array
int[] TheList = new int[OrderedListA+OrderedListB];
System.out.println(Arrays.toString(TheList));
}
}

Thank you.

Comments
Post Details
Added on Sep 19 2021
0 comments
2 views