trying to learn and practice arrays but I have a problem with this small example. Can someone please help me please? Thank you
public class Homework1{
public static void main(String[] args){
int[] anArray={ 1,2,3,4,5,6,7,8,9};
System.out.println(anArray.length);
int min=0;
int max=0;
for(int i=0; i<anArray.length; i++){
if(anArray[i].compareTo(anArray[min]) < 0)
min=i;
if(anArray[i].compareTo(anArray[max]) > 0)
max=i;
}
System.out.println(max);
System.out.println(min);
}
}
And I am getting this error message:
int cannot be dereferenced
if(anArray[i].compareTo(anArray[min]) < 0)
int cannot be dereferenced
if(anArray[i].compareTo(anArray[max]) > 0)
Thank you.
CompareTo will not work with Premitive type. You can use comparator operator.