有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何从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();
        }
    }

共 (1) 个答案

  1. # 1 楼答案

    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();
    }