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

1 Answers

0
Roshana Pitigala On

Java has some in-built tools, it's easier to use them rather than writing your own algorithm for simple things.

Use a LinkedHashMap to store data as key value pairs. Then iterate the map, and insert them into another map by comparing the values using a Comparator.

LinkedHashMap<String, Integer> students = new LinkedHashMap<>(),
        sortedStudents = new LinkedHashMap<>();

students.put("Student0", 34);
students.put("Student1", 28);
students.put("Student2", 20);
students.put("Student3", 31);
students.put("Student4", 32);
students.put("Student5", 28);
students.put("Student6", 37);
students.put("Student7", 41);

List<Map.Entry<String, Integer>> list = new ArrayList<>(students.entrySet());
list.sort(Entry.comparingByValue(new Comparator<Integer>() {
    public int compare(Integer o1, Integer o2) {
        if (o1.intValue() < o2.intValue()) {
            return 1;
        } else if (o1.intValue() > o2.intValue()) {
            return -1;
        }
        return 0;
    }
}));

for (Entry<String, Integer> entry : list) {
    sortedStudents.put(entry.getKey(), entry.getValue());
}

System.out.println(sortedStudents);

Output:

{Student7=41, Student6=37, Student0=34, Student4=32, Student3=31, Student1=28, Student5=28, Student2=20}