I have a list [0, 0, 1, 0, 1, 2, 0, 2, 3]
and I want to find a way to generate an error if a number is repeated in the list.
So, I know that Collections.frequency
can count how many time a number is found in a list.
if (Collections.frequency((Collection<?>) smallerLists.toArray()[0], ((ArrayList<Integer>) smallerLists.toArray()[0]).get(0)) > 1) ;
System.out.println("ERREUR : Le nombre " + ((ArrayList<Integer>) smallerLists.toArray()[0]).get(0) + " apparaît plus d'une fois sur la ligne 1");
if (Collections.frequency((Collection<?>) smallerLists.toArray()[0], ((ArrayList<Integer>) smallerLists.toArray()[0]).get(1)) > 1) ;
System.out.println("ERREUR : Le nombre " + ((ArrayList<Integer>) smallerLists.toArray()[0]).get(1) + " apparaît plus d'une fois sur la ligne 1");
if (Collections.frequency((Collection<?>) smallerLists.toArray()[0], ((ArrayList<Integer>) smallerLists.toArray()[0]).get(2)) > 1) ;
System.out.println("ERREUR : Le nombre " + ((ArrayList<Integer>) smallerLists.toArray()[0]).get(2) + " apparaît plus d'une fois sur la ligne 1");
if (Collections.frequency((Collection<?>) smallerLists.toArray()[0], ((ArrayList<Integer>) smallerLists.toArray()[0]).get(3)) > 1) ;
System.out.println("ERREUR : Le nombre " + ((ArrayList<Integer>) smallerLists.toArray()[0]).get(3) + " apparaît plus d'une fois sur la ligne 1");
if (Collections.frequency((Collection<?>) smallerLists.toArray()[0], ((ArrayList<Integer>) smallerLists.toArray()[0]).get(4)) > 1) ;
System.out.println("ERREUR : Le nombre " + ((ArrayList<Integer>) smallerLists.toArray()[0]).get(4) + " apparaît plus d'une fois sur la ligne 1");
if (Collections.frequency((Collection<?>) smallerLists.toArray()[0], ((ArrayList<Integer>) smallerLists.toArray()[0]).get(5)) > 1) ;
System.out.println("ERREUR : Le nombre " + ((ArrayList<Integer>) smallerLists.toArray()[0]).get(5) + " apparaît plus d'une fois sur la ligne 1");
if (Collections.frequency((Collection<?>) smallerLists.toArray()[0], ((ArrayList<Integer>) smallerLists.toArray()[0]).get(6)) > 1) ;
System.out.println("ERREUR : Le nombre " + ((ArrayList<Integer>) smallerLists.toArray()[0]).get(6) + " apparaît plus d'une fois sur la ligne 1");
if (Collections.frequency((Collection<?>) smallerLists.toArray()[0], ((ArrayList<Integer>) smallerLists.toArray()[0]).get(7)) > 1) ;
System.out.println("ERREUR : Le nombre " + ((ArrayList<Integer>) smallerLists.toArray()[0]).get(7) + " apparaît plus d'une fois sur la ligne 1");
if (Collections.frequency((Collection<?>) smallerLists.toArray()[0], ((ArrayList<Integer>) smallerLists.toArray()[0]).get(8)) > 1) ;
System.out.println("ERREUR : Le nombre " + ((ArrayList<Integer>) smallerLists.toArray()[0]).get(8) + " apparaît plus d'une fois sur la ligne 1");
I know this code is heavy and I'm sure there's an easier way to do it (an iteration maybe? I'm still very new at programming). The easier way would be to generate an exception by making a constructor but I'm stuck.. Thanks !