Итак, у меня есть метод, в котором я передаю ArrayList идее этого метода - взять вход и разбить каждый набор строк соответствия в свой отдельный список. Вот какой-то псевдокод.Для путаницы путаницы, почему это не цикл?
Array = (a,a,a,b,c,d,d,e,e,e,e,e,f,f,s)
Так что я хочу, чтобы этот алгоритм должен был разбить этот массив на двумерный массив с равными элементами. Как это.
A[0][] = (a,a,a)
A[1][] = (b)
A[2][] = (c)
A[3][] = (d,d)
A[4][] = (e,e,e,e,e)
A[5][] = (f,f)
A[6][] = (s)
Так что я пытался сделать, это положить его в цикле, что проверить дополнительный элемент вперед, чтобы увидеть, если он НЕ равен, то он знает, T
int equalStringGroupIndex = 0;
int i = 0;
for(int first = 0, second = 0 ; input.get(first).equals(input.get(second)); second++){
equalStringGroups[equalStringGroupIndex][i] = input.get(second);
i++;
//This if statment checks the element ahead then equals first = second, But when it jumps back to the top of the loop in the debugger it does'nt seem to check it even though in my Watches it's True
if(!input.get(first).equals(input.get(second + 1))){
equalStringGroupIndex++;
i = 0;
first = second;
}
}
Почему это не цикл после добавления первого набора 'a' в 2D-массив Спасибо.
ОБНОВЛЕНИЕ: Спасибо за помощь, я решил пойти по маршруту HashMap. Вот что я придумал. Кажется, он работает.
private HashMap<String, Integer> countDuplicates(ArrayList<String> input){
HashMap<String, Integer> duplicates = new HashMap<>();
//Value init loop, sets all values to 0;
for (String s : input){
Integer valueInitVar = 0;
duplicates.put(s, valueInitVar);
}
//Increases the value by 1 each time the same key is encountered;
for (String s : input){
Integer tempDuplicateAmount = duplicates.get(s);
//I could use the '++' operator but I feel ' var += 1' is much nicer to read;
tempDuplicateAmount += 1;
duplicates.put(s, tempDuplicateAmount);
}
return duplicates;
}
Что такое 'input'? –
Сортированный массив строк – Definity
И почему это * что * происходит? –