如何从 java 中的向量向量打印

How do I print from a vector of vectors in java

我刚开始Java编程。我正在做编程挑战书中的一些问题。问题是打印出给定整数集的最大循环长度。这是我的代码:

public class JavaApplication9 {

    /**
     * @param args the command line arguments
     */   

    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("This is a program that calculates the maximum"
                + " cyclelength of two integers between 1 and 1000000");
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        int x = keyboard.nextInt();
        System.out.println();
        System.out.print("Enter the second number: ");
        int y = keyboard.nextInt();
        @SuppressWarnings("UseOfObsoleteCollectionType")
        Vector<Integer> cycleVec = new Vector<Integer>();
        Vector newVec = new Vector(); 
        if (x <= 0 || y <= 0 || x >= 1000000 || y >= 1000000)
            System.out.println("Wrong input. Input must be greater than 0"
               + " and less than 1,000,000!!");
        else{
            for(int k = Math.min(x, y);k<=Math.max(x, y);k++){
                   // We have to count 1 also
                   cycleVec.add(1 + cycleLength(k));  
                   newVec.add(numList(k));
            }
            Enumeration vEnum = cycleVec.elements();
            while(vEnum.hasMoreElements()){
                int a = (int) vEnum.nextElement();
                display(newVec, x, y, a);
            }
            System.out.println("\nElements in vector of cyclelengths:");
            while(vEnum.hasMoreElements())
                System.out.print(vEnum.nextElement() + " ");
            System.out.println();
            Object obj = Collections.max(cycleVec);
            System.out.printf("%d %d ", x, y);
            System.out.println(obj);
        }
    }

    public static int cycleLength(int x){
        int termPoint = 0;
        int count = 0;
        while (termPoint != 1){
            if (x % 2 == 0){
                x = x/2;
                termPoint = x;
            }else{
                x = x*3 + 1;
                termPoint = x;
            }
            ++count;
        }
        return count;
    }

    public static Vector numList(int x){
        int termPoint = 0;
        Vector vec = new Vector();
        while (termPoint != 1){
            if (x % 2 == 0){
                x = x/2;
                termPoint = x;
            }else{
                x = x*3 + 1;
                termPoint = x;
            }
            vec.addElement(x);
        }
        return vec;
    }

    public static void display(Vector v, int x, int y, int size){
        System.out.println("Elements generated:");
        int m = 0;
        for(int k = Math.min(x, y); k <= Math.max(x, y); k++){
            System.out.print("Number " + k + " ");
            for (int i = 0; i < v.size(); i++){
                for (int j = 0; j < size; j++){
                    m = (int)((Vector)v.elementAt(i)).elementAt(j);
                    System.out.print(m + " ");
                }
                System.out.println();
            }
        }
    }
}

输出为

run:
This is a program that calculates the maximum cyclelength of two integers between 1 and 1000000
Enter the first number: 201

Enter the second number: 210
Elements generated:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18 >= 18
    at java.util.Vector.elementAt(Vector.java:474)
Number 201604 302 151 454 227 682 341 1024 512 256 128 64 32 16 8 4 2 1     at javaapplication9.JavaApplication9.display(JavaApplication9.java:95)
    at javaapplication9.JavaApplication9.main(JavaApplication9.java:44)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

代码的第一部分有效(如果删除显示功能)。我只是想为每个整数 x 打印出生成的整数列表以及循环长度列表和最大循环长度。还有什么方法可以让这段代码更高效(减少代码行数)?

显示的新代码(有效)

 public static void display(Vector v){
        System.out.println("Elements generated:");
        int m = 0, w = 0;     
        for (int i = 0; i < v.size(); i++){
            Vector inner = (Vector)v.elementAt(i);
            System.out.println("Number ");
            for (int j = 0; j < inner.size(); j++){
                m = (int)inner.elementAt(j);
                if (j == 0){
                    if (m % 2 == 0){
                        w = (m-1)/3;
                    }else{
                        w = m * 2;
                    }
                System.out.print(w + " ");
                }
                System.out.print(m + " ");
            }
            System.out.println();
        }
    }

display 方法的最后一个参数是一个整数,用作内部向量的大小,但是回到顶部我们看到它实际上比大小大一个,因为这一行:

// We have to count 1 also
cycleVec.add(1 + cycleLength(k));

您需要考虑 cycleVec 值用于在 display 方法中循环遍历向量的正确限制。

该异常意味着您正试图访问一个向量中比它拥有的条目更多的条目。您在同一行上对 elementAt 进行了两次调用。看起来这是失败的第二个参考。也就是说j太大了,也就是说size参数不对。访问内部数组的安全方法是这样的:

for (int i = 0; i < v.size(); i++){
    Vector inner = (Vector)v.elementAt(i);
    for (int j = 0; j < inner.size(); j++) {
        m = (int)inner.elementAt(j);
        System.out.print(m + " ");
    }
    System.out.println();
}