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

如何从java中的向量向量打印,java,vector,collections,Java,Vector,Collections,我刚开始Java编程。我正在做编程挑战书中的一些问题。问题是打印出给定整数集的最大循环长度。这是我的密码: public class JavaApplication9 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here

我刚开始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();
            }
        }
    }
}
代码的第一部分起作用(如果删除显示功能)。我只想打印出每个整数x,生成的整数列表,循环长度列表和最大循环长度。还有什么方法可以使这段代码更高效(减少代码行数)

显示屏的新代码(正常工作)

公共静态无效显示(矢量v){
System.out.println(“生成的元素:”);
int m=0,w=0;
对于(int i=0;i
display
方法的最后一个参数是一个用作内部向量大小的整数,但是在顶部我们看到它实际上比大小大一个,因为这行:

// We have to count 1 also
cycleVec.add(1 + cycleLength(k));
您需要考虑在
display
方法中循环通过向量的正确限制中,使用了哪些
cycleVec
值进行循环

该异常意味着您试图访问向量中比它拥有的更多的条目。在同一行中有两个对
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();
}
for(int i=0;i
使用
向量的任何原因
?它在每一个动作上都是同步的,甚至看起来你都不是多线程的。来自:“Vector是同步的。如果不需要线程安全实现,建议使用ArrayList代替Vector。”谢谢。那么如何使用ArrayList呢?就像向量一样。谢谢。cycleVec值保存每个cycleLength操作的计数。所以我把这些值存储在一个向量中,这个向量是newVec的一个元素。基本上,newVec.size()是行,cycleVec的每个元素都是列数。输出为:输入第一个数字:10输入第二个数字:生成的30个元素:数字105 16 8 4 2 1 34 17 52 26 13 40 6 3 10 5 16 20 10 5 16 8 7 22 11 34 17 52 46 23 70 35 106 53线程“main”java.lang.ArrayIndexOutOfBoundsException:4>=4 8 4 2 1 at java.util.Vector.elementAt(Vector.java:474)在javaapplication9.javaapplication9.display(javaapplication9.java:95)在javaapplication9.javaapplication9.main(javaapplication9.java:44)java结果:1在答案中添加更多信息
// We have to count 1 also
cycleVec.add(1 + cycleLength(k));
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();
}