-4

it sounds crazy I know. I'm newbie at programming and having trouble with somethings. Lately, I've been doing some arrays and I know how to check equals two other arrays like using Arrays.equals(object, object) but what I mean is I am working on 1 array. I want to check equals if the array is after sorted and before sorted.

System.out.println("your array before sorted: " + Arrays.toString(size));
//System.out.println("total of the elements: " +sum);
Arrays.sort(size);
System.out.println("your array after sorted: "+ Arrays.toString(size)); 
if(Arrays.equals(size, size)) {
    System.out.println("true af ");
}

I tried to create a variable which stores the sorted array but it gives my an error cannot convert from void to int or something" because Arrays.sort(object) function returns void. Any help would be appreciated.

RamPrakash
  • 1,687
  • 3
  • 20
  • 25
toogi
  • 11
  • 4

1 Answers1

0

Have to copy contents of array into another array in order to compare otherwise you end up refering to same array.

import java.util.Arrays;

public class Test {

    public static void main(String[] args) {

        int[] sizebefore = { 3, 5, 1, 8 };
        int[] sizeafter = new int[sizebefore.length];
        System.arraycopy(sizebefore, 0, sizeafter, 0, sizebefore.length);

        Arrays.sort(sizeafter);
        System.out.println("your array before sorted: " + Arrays.toString(sizebefore));
        System.out.println("your array after sorted: " + Arrays.toString(sizeafter));

        if (Arrays.equals(sizebefore, sizeafter)) {
            System.out.println("Matches after sort ");
        } else {
            System.out.println("Doesn't match after sort ");
        }
    }

}
RamPrakash
  • 1,687
  • 3
  • 20
  • 25