Java将错误的数据写入数组

时间:2015-01-14 16:22:39

标签: java arrays

我正在实施算法,该算法将计算链表中唯一数字的外观。当输入类似于1 1 1 2 2 2

时,问题在于我在计算外观的for循环中

而不是获取输出

  

1次出现3次
  2次出场3次

我正在

  

1外观3
  0出现0

输入1 2 3 4 5 6 7 8 9我得到了索引异常的数组。这是代码

public static void main(String[] args) {
    LinkedList<Integer> list = new LinkedList<Integer>();
    Scanner user_input = new Scanner(System.in);
    String input = user_input.nextLine();
    //Getting user input, if the user enter an empty line(enter, enter) the loop will die
    while(input.length() > 0){
        list.add(Integer.parseInt(input));
        input = user_input.nextLine();
    }
    Collections.sort(list); //sorting the input
    //Getting the number of unique numbers
    int count_of_unique_numbers = 1;// There will be atleast one unique number

    if(!list.isEmpty()){
        int temp = list.get(0);
        for(int i = 1;i < list.size(); i++){
            if(temp != list.get(i)){
                count_of_unique_numbers++;
                temp = list.get(i);
            }
        }
    }
    else{
        System.out.println("The list is empty"); 
        return;
    } 
    //Counting how many times the unique numbers apper;
    int number_appearance[][] = new int[count_of_unique_numbers][2];
    int temp = list.get(0);
    int counter = 1;
    int j = 0;
    for(int i = 1;i < list.size();i++){
        if(temp == list.get(i)){ counter++; }
        else{
            number_appearance[j][0] = temp;
            number_appearance[j][1] = counter;
            counter = 1;
            temp = list.get(i);
            j++;
        }
    }
    //Printing the number_appearance array
    for(int i = 0; i < count_of_unique_numbers; i++){
        System.out.println("The number: " + number_appearance[i][0] + " appearece " + number_appearance[i][1] + " times");
    }
}

4 个答案:

答案 0 :(得分:1)

你可以简单地完成所有这些。

Map < String, Integer > numMap = new HashMap < String, Integer > ();
Scanner user_input = new Scanner(System. in );
String input = user_input.nextLine();

String[] inputArray = input.split(" ");
for (String s: inputArray) {
    if (numMap.containsKey(s)) {
        numMap.put(s, numMap.get(s) + 1);
    } else {
        numMap.put(s, 1);
    }
}

for (Map.Entry < String, Integer > entry: numMap.entrySet()) {
    System.out.println("number: " + entry.getKey() + " appeared: " + entry.getValue() + " times");
}

答案 1 :(得分:0)

你可以简单地做

List<Integer> foundNumbers = new ArrayList<Integer>();
for(int i = 0; i < list.size(); i++)
{
    if(foundNumbers.contains(list.get(i))
    {
        System.out.println(list.get(i) + " appearance " + Collections.frequency(list, list.get(i) + " times.");
        foundNumbers.add(list.get(i));
    }
}

请注意,自JDK 1.6以来,Collections.frequency可用

答案 2 :(得分:0)

我知道这不是你原本想要做的,但使用Map

更容易做到
public static void main(String[] args) {
    LinkedList<Integer> list = new LinkedList<Integer>();
    Scanner user_input = new Scanner(System.in);
    String input = user_input.nextLine();
    // Getting user input, if the user enter an empty line(enter, enter) the loop will die
    while (input.length() > 0) {
        list.add(Integer.parseInt(input));
        input = user_input.nextLine();
    }
    Collections.sort(list); // sorting the input
    Map<Integer, Integer> count = new HashMap<Integer, Integer>();
    for (int i : list) {
        if (count.containsKey(i)) {
            count.put(i, count.get(i) + 1);
        }
        else {
            count.put(i, 1);
        }
    }

    for (Map.Entry<Integer, Integer> num : count.entrySet()) {
        System.out.println("The number: " + num.getKey() + " appearece " + num.getValue() + " times");
    }
}

答案 3 :(得分:0)

您可以通过添加最终号码的出现次数来解决问题:

for(int i = 1;i < list.size();i++){
    if(temp == list.get(i)){ counter++; }
    else{
        System.out.println(temp+":"+counter);
        number_appearance[j][0] = temp;
        number_appearance[j][1] = counter;
        counter = 1;
        temp = list.get(i);
        j++;
    }
}
number_appearance[j][0] = temp; // added
number_appearance[j][1] = counter; // added

输入:

1 1 1 2 2 2

输出:

The number: 1 appearece 3 times
The number: 2 appearece 3 times

输入:

1 2 3 4 5 6 7 8 9

输出:

The number: 1 appearece 1 times
The number: 2 appearece 1 times
The number: 3 appearece 1 times
The number: 4 appearece 1 times
The number: 5 appearece 1 times
The number: 6 appearece 1 times
The number: 7 appearece 1 times
The number: 8 appearece 1 times
The number: 9 appearece 1 times