简化多线阵列

时间:2013-11-17 22:29:44

标签: java arrays

我还是Java的新手,并且想知道是否有办法减少我的阵列以利用余数运算符在5进入数字时开始换行并且没有遗留的东西?从今天早上开始,我一直试图解决这个问题,似乎没有取得任何进展。提前谢谢你的帮助。

    public class arrayTest {

        public static void main(String args[]) { 
            final int ARRAY_LENGTH = 25;           // number of ints 
            int array[] = new int[ ARRAY_LENGTH ]; // calculate value for each array element 

            for (int counter = 0; counter < 5; counter++ ) 
                System.out.printf("%d, ", counter); 
            for (int counter = 5; counter < 6; counter++ ) 
                System.out.printf("%d\n", counter);
            for (int counter = 6; counter < 10; counter++ ) 
                System.out.printf("%d, ", counter);
            for (int counter = 10; counter < 11; counter++ ) 
                System.out.printf("%d\n", counter);
            for (int counter = 11; counter < 15; counter++ ) 
                System.out.printf("%d, ", counter);
            for (int counter = 15; counter < 16; counter++ ) 
                System.out.printf("%d\n", counter);
            for (int counter = 16; counter < 20; counter++ ) 
                System.out.printf("%d, ", counter);
            for (int counter = 20; counter < 21; counter++ ) 
                System.out.printf("%d\n", counter);
            for (int counter = 21; counter < 25; counter++ ) 
                System.out.printf("%d, ", counter);
            for (int counter = 25; counter < 26; counter++ ) 
                System.out.printf("%d\n", counter);
        } 

    } 

4 个答案:

答案 0 :(得分:1)

public class arrayTest
{

public static void main( String args[] )
{
  final int ARRAY_LENGTH = 25; // number of ints 
  int array[] = new int[ ARRAY_LENGTH ];// calculate value for each array element 
  for (int counter = 0; counter < array.length; counter++ ) {
    System.out.printf("%d, ", counter); 
    if (counter%5 == 4) {
    System.out.printf("\n"); // System.out.println() also works
    }
  }
}

}

答案 1 :(得分:0)

初始化数组后使用此功能

for(int i=0; i<ARRAY_LENGTH;i+=5){
   for(int counter=i;counter<i+5;counter++){
      array[counter]=counter;
      System.out.print(counter);
   }
   System.out.print("\n");
}

答案 2 :(得分:0)

您只需按如下方式使用一个循环:

final int ARRAY_LENGTH = 25;
int[] array = new int[ARRAY_LENGTH];
for(int i = 1; i <= ARRAY_LENGTH; i++){
    array[i - 1] = i % 5;
    System.out.println(i % 5);

答案 3 :(得分:0)

将for循环替换为(与程序相同的输出),代码中的注释:

public class arrayTest {

    public static void main(String args[]) { 
        final int ARRAY_LENGTH = 25;           // number of ints 
        int array[] = new int[ ARRAY_LENGTH ]; // calculate value for each array element 

        for (int counter = 0; counter < ARRAY_LENGTH; counter++ ) {
            array[counter] = counter;
            System.out.printf("%d, ", counter);

            //skip 0, then if current number modulo 5 is zero (no remainder)
            //insert new line
            if (counter > 0  && counter % 5 == 0) System.out.println();
        }
        //to add 25 at the end, as in your code, you can do this:
        //this is because our loop starts at 0 and goes until 24
        System.out.println(ARRAY_LENGTH);
    } 
}