Java 从其他方法打印ArrayList

Java 从其他方法打印ArrayList,java,arraylist,printf,Java,Arraylist,Printf,设计一个函数,其职责是很好地显示素数数组。它应该每行显示10个数组的内容。需要打印和打印LN的组合。显示每个 宽度为7的字段中的数字(使用printf) 这是我的资料,但我不确定是否正确 public static void printArray(ArrayList<Integer> primes){ System.out.printf("%7s", primes); if (prrimeCount % 10 == 0){ System.out.pr

设计一个函数,其职责是很好地显示素数数组。它应该每行显示10个数组的内容。需要打印和打印LN的组合。显示每个 宽度为7的字段中的数字(使用printf)

这是我的资料,但我不确定是否正确

public  static void printArray(ArrayList<Integer> primes){
    System.out.printf("%7s", primes);
    if (prrimeCount % 10 == 0){
        System.out.println();
    }
}
}
publicstaticvoidprintary(arraylistprimes){
System.out.printf(“%7s”,素数);
如果(prrimeCount%10==0){
System.out.println();
}
}
}
这将引发异常
primes
Arraylist
%7s
需要
字符串

公共静态无效打印数组(Arraylist primes){
public  static void printArray(ArrayList<Integer> primes){
    // You need to have a counter as you iterate over the list
    int count = 1;
    // Integer is autoboxed into an int when interating over prime
    for(int prime : primes){
        // "%7d" instead of "%7s" as d is used for integer but s is used for strings
        System.out.printf("%7d ", prime);
        // count++ will icrement count after this statement is called
        if (count++ % 10 == 0){
            System.out.println();
        }
    }
}
//在遍历列表时,需要有一个计数器 整数计数=1; //整数在素数上交互时自动装箱为int for(整数素数:素数){ //“%7d”而不是“%7s”,因为d用于整数,s用于字符串 System.out.printf(“%7d”,素数); //count++将在调用此语句后执行icrement count 如果(计数+++%10==0){ System.out.println(); } } }
我认为这是不对的。我相信问题是要求您显示primes
ArrayList
中的元素。要做到这一点,您应该考虑使用<<代码> < /CODE>循环来遍历所有元素。要显示每行10,您可能需要使用
%
确定循环增量是否可被10整除。不确定是否正确?运行它,看看它是否打印出说明书上说它应该打印的内容。
public  static void printArray(ArrayList<Integer> primes){
    // You need to have a counter as you iterate over the list
    int count = 1;
    // Integer is autoboxed into an int when interating over prime
    for(int prime : primes){
        // "%7d" instead of "%7s" as d is used for integer but s is used for strings
        System.out.printf("%7d ", prime);
        // count++ will icrement count after this statement is called
        if (count++ % 10 == 0){
            System.out.println();
        }
    }
}