Я пытаюсь объединить три массива в один массив. Только соблюдение общих элементов. Это не дублирующий вопрос. Я знаю, что есть другие примеры в Интернете, но это использование int [], и я не знаю, как это сделать с помощью Comparable.Как объединить общие элементы в один массив?
Что мне нужна помощь с:
Как добавить единый комбинированный/обновленный массив в 2d массив.
Как подсчитать итерации каждый раз, когда элемент сравнивается.
Если бы я хотел, как я могу изменить массивы, которые у меня есть сейчас в списке? - Я думал, может быть, это было бы легче добавить.
Я новичок в программировании, и я был бы признателен за помощь. Я пытаюсь изучить java, просто прочитав книги и поиская в Интернете.
Это то, что у меня есть до сих пор.
public class Common{
Comparable [] col_1 = {1, 1, 2};
Comparable [] col_2 = {1, 1, 2,3};
Comparable [] col_3= {1, 1, 2,3,4,};
Comparable [][] collections = {col_1, col_2, col_3};
int comparisonCount = 0
public Comparable[] findCommon(Comparable [][] collections){
int i, j, k, x, y;
for(i = 0; i< col_1.length; i++){
for(j = 0; j < col_2.length; j++){
for(k = 0; k < col_3.length; k++){
comparisonCount++;
// This should be counting but is not...
if(col_1[i].compareTo(col_2[j]) == 0 && col_1[i].compareTo(col_3[k]) ==0){
//keep searching until last element & allow duplicates & add to collections or a temp[]
}
}
}
}
// Here I'm not sure how to add the elements to the collection
for (x = 0; x < collections.length; x++){
for(y = 0; y< collections[x].length; y++){
collections [x][y] = ?????? // not sure how to add results here
}
}
}
public void setComparisons(int count){
count = comparisonCount;
}
public int getComparisons(){
return comparisonCount;
}
public class Sorting {
public static void main(String[] args) {
Common m = new Common();
//I want to test it from here but I don't know how to initialize each array.
for(int x=0; x < m.collections.length; x++){
for(int y= 0; y< m.collections[x].length; y++){
System.out.println(m.collections[x][y]);
}
// what I should be getting is only (1, 1, 2) - the order is not important really. I just want to learn.
}
System.out.println(m.getComparisons());
}
}
@Herman Ганди спасибо. –