数组列表方法给出错误的输出

时间:2015-10-11 12:35:54

标签: java arrays list methods arraylist

我有这个功课,我们使用数组的包实现。添加/删除等工作正常,直到我运行列表选项。基本上我应该在包中有一个数字表和它们出现的次数,但我得到一些完全不同且无关的东西。

我应该拥有的东西:

我真正得到的是:

这是代码的一部分:

import java.util.Scanner;

public class Bag {
int index = 0;
int[] array = new int[100];

public static void main(String[] args) {
    int x = 0;
    Bag bag = new Bag();

    Scanner scan = new Scanner(System.in);

    while (x == 0) {
        System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> ");
            char chr = scan.next().charAt(0);
            if (chr == 'A') {
                int b = scan.nextInt();
                bag.Add(b);
            }
            if (chr == 'D') {
                int b = scan.nextInt();
                bag.Delete(b);
            }
            if (chr == 'F') {
                int b = scan.nextInt();
                bag.Find(b);
            }
            if (chr == 'S') {
                bag.Size();
            }
            if (chr == 'm') {
                bag.Min();
            }
            if (chr == 'M') {
                bag.Max();
            }
            if (chr == 'L') {
                bag.List();
            }
            if (chr == 'Q') {
                x++;
                bag.Quit();
            }
    }


}
public int Occurs(int a) {
    int NumberofElements = 0;
    for (int i = 0; i <= index; i++) {
        if (array[i] == a); {
            NumberofElements++;
        }
    }
    return NumberofElements;
}
public void Add(int a) {
    array[index] = a;
    index++;
    System.out.println("  " + a + " is added to the bag. ");
}
public void Delete(int a) {
    for (int i = 0; i < index; i++) {
    if (a == array[i]) {
        while (i < index) {
            array[i] = array[i + 1];
            i++;
        }
        System.out.println("  " + a + " is deleted from the bag");
        index--;
        return;
    }
}
System.out.println(" cannot delete " + a + ", does not exist in the bag");

}
public void Find(int a) {
    if (Occurs(a) == 0)
        System.out.println("  " + a + "does not exist in the bag");
    else {
        System.out.println("  there is (" + Occurs(a) + " in the bag");
    }
}
public void Size() {
    System.out.println("  there are " + index + " numbers in the bag" );
}
public void Min() {
    int min = array[0];
    for (int i = 1; i < index; i++) {
        if (min > array[i]) {
            min = array[i];
        }
    }
    System.out.println("  the minimum number in the bag is " + min);
}
public void Max() {
    int max = array[0];
    for (int i = 1; i < index; i++) {
        if (max < array[i]) {
            max = array[i];
        }
    }
    System.out.println("  the maximum number in the bag is " + max);
}
public void delete (int a) {
    for (int i = 0; i < index; i++) {
        if (a == array[i]) {
            while (i < index) {
                array[i] = array[i + 1];
                i++;
            }
            index--;
            return;
        }
    }
}
public void List() {
    System.out.println("+--------+--------+");
    System.out.println("| Number | Occurs |");
    System.out.println("+--------+--------+");
    int temp[] = array;
    for (int i = 0; i < index; i++) {
        if(Occurs(array[i]) == 1) {
            System.out.printf("|%8d|%8d|\n", array[i], Occurs (array[i]));
            System.out.println("+--------+--------+");
        }
        else {
            System.out.printf("|%8d|%8d|\n", array[i], Occurs(array[i]));
            System.out.println("+--------+--------+");
            for (int j = 0; j <= Occurs(array[i]); j++) {
                delete(array[i]);
            }
        }
    }
    array = temp;

}
public void Quit() {
    System.out.println("bye!");
}

}

1 个答案:

答案 0 :(得分:0)

以下是您的Occurs方法中的错误:

    for (int i = 0; i <= index; i++) {

实际应该替换为:

    for (int i = 0; i < index; i++) {

因为我们要测试索引下的值,而不是索引。

在这一行:

        if (array[i] == a); {

你在错误的地方写了一个拼写错误:

        if (array[i] == a) {

以下是对List方法的更正:

public void List() {
    System.out.println("+--------+--------+");
    System.out.println("| Number | Occurs |");
    System.out.println("+--------+--------+");
    List<Integer> alreadyUsed = new ArrayList<Integer>();
    for (int i = 0 ; i < index ; i++){
        if (!alreadyUsed.contains(array[i])){
            System.out.printf("|%8d|%8d|\n", array[i], Occurs(array[i]));
            System.out.println("+--------+--------+");
            alreadyUsed.add(array[i]);
        }       
    }
}

我使用ArrayList知道我在数组中遇到过哪个值,这样我就不会错误地计算它们两次。

以下是输出的示例:

Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
23
  23 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
12
  12 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
23
  23 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
23
  23 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
46
  46 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
12
  12 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
23
  23 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> L
+--------+--------+
| Number | Occurs |
+--------+--------+
|      23|       4|
+--------+--------+
|      12|       2|
+--------+--------+
|      46|       1|
+--------+--------+
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >>