Porównywanie tablic (equals?)

0

Czesc. Chcialbym w jakis sposob porównać dwie tablice, i jeśli jakakolwiek wartość się powtarza to wydrukować tekst. Próbowałem przez equals, przez pętle. Kurczę, nic.

public static void main(String[] args) {
        int wynik[];
        wynik = losuj(5,10);
        Arrays.sort(wynik);
        System.out.println("Wyniki: " + Arrays.toString(wynik));
        int rezultat[];
        rezultat = wybierz(5,10);
        Arrays.sort(rezultat);
        System.out.println("Wyniki: " + Arrays.toString(rezultat));
        if (Arrays.equals(rezultat, wynik)){
            System.out.println("NICE ONE");
        }
0

z javaDoc

boolean equals(Object o) (dla List)

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

0

Google: "how to compare two int arrays in java".
Pierwszy wynik: http://stackoverflow.com/questions/14897366/comparing-two-integer-arrays-in-java

public static void compareArrays(int[] array1, int[] array2) {
    boolean b = true;
    if (array1 != null && array2 != null){
        if (array1.length != array2.length)
            b = false;
        else
            for (int i = 0; i < array2.length; i++) {
                if (array2[i] != array1[i]) {
                    b = false;
                }
            }
    }else{
        b = false;
    }
    System.out.println(b);
}
0

Chodzi o takie coś?

public static void main(String[] args) {
    Integer wynik[];
    wynik = losuj(5,10);
    Arrays.sort(wynik);
    System.out.println("Wyniki: " + Arrays.toString(wynik));
    Integer rezultat[];
    rezultat = wybierz(5,10);
    Arrays.sort(rezultat);
    System.out.println("Wyniki: " + Arrays.toString(rezultat));
    ArrayList<Integer> listaWynik = new ArrayList<Integer>(Arrays.asList(wynik));
    if (listaWynik.contains(rezultat))
      System.out.println("NICE ONE");
  }
0

A jeśli indeksy mają się zgadzać to może tak?

public static void main(String[] args) {
    Integer wynik[];
    wynik = losuj(5,10);
    Arrays.sort(wynik);
    System.out.println("Wyniki: " + Arrays.toString(wynik));
    Integer rezultat[];
    rezultat = wybierz(5,10);
    Arrays.sort(rezultat);
    System.out.println("Wyniki: " + Arrays.toString(rezultat));
    int i = wynik.length < rezultat.length ? wynik.length : rezultat.length;
    for (int j = 0; j < i; j++)
      if (wynik[j] != null && rezultat[j] != null && wynik[j].equals(rezultat[j]))
        System.out.println("NICE ONE");
  }
1

Lol, ale kombinujecie.

true, jeśli powtarza się dowolna wartość, bez względu na indeks:

public boolean compare(int[] array1, int[] array2) {
	for (int i = 0; i < array1.length; i++) {
		for (int j = 0; j < array2.length; j++) {
			if (array1[i] == array2[j]) {
				return true;
			}
		}
	}
	return false;
}

true, jeśli powtarza się wartość w tym samym indeksie:

public boolean compare(int[] array1, int[] array2) {
	for (int i = 0; i < array1.length && i < array2.length; i++) {
		if (array1[i] == array2[i]) {
			return true;
		}
	}
	return false;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1