So basically I need to make an array containing time spent studying. The first task was to get the total and store them in the array which I done,
//student number total hours
Student Weekly study
Student0 34
Student1 28
Student2 20
Student3 31
Student4 32
Student5 28
Student6 37
Student7 41
the second task was to arrange the students by the longest hours. I first arranged the actual hours studied in an array:
public static void sort(Integer[] array){
Integer studentNumber [] = new Integer[8];
Integer temp[] = new Integer[8];
for (int i = 0; i < array.length;i++){
temp[i] = array[i];//declaring value of index so it doesn't change
}
Arrays.sort(array, Collections.reverseOrder());//sorted the original array in descending order
}
i then needed to display the student (identifying them by their orginal index number e.g. student0 has 34) so I made a loop to compares the two value if it does then it will use the index of the of 'temp':
for (int i = 0; i < temp.length;i++){
for (int j = 0; j < array.length;j++){
if (array[i].equals(temp[j] )){
System.out.println("Student" + j + "\t " + array[i]);
break;
}
}
output:
Student7 41
Student6 37
Student0 34
Student4 32
Student3 31
Student1 28
Student1 28
Student2 20
as you can see it shows student1 twice when it supposed to show supposed to show 5 after however because they had the same value it looks for the first thing that is equal to it.
I was looking for solutions and I was trying to make a variable to check whether the index has already been visited:
int pointer =0;
for (int i = 0; i < temp.length;i++){
for (int j = 0; j < array.length;j++){
if (array[i].equals(temp[j] )&& i > pointer){
System.out.println("Student" + j + "\t " + array[i]);
break;
}
}
pointer++;
}
so my question is, is there a way to check/skip an index with duplicate values that have already been visited