简体   繁体   中英

Better way to write a for loop over and over?

I'm working on getting better at programming Java, I'm a new beginner and figured how to write basic programs and basic data structures in my free time before I venture to college next year!

So is there a better way to write this code here?

                int p;
                for (p = 0; p < 10; p++) {
                    System.out.print(time[p] + ",");

                }
                System.out.println();
                System.out.println();
                System.out.println();
                for (p = 0; p < 10; p++) {
                    System.out.print(something[p] + ",");
                }
                System.out.println();
                System.out.println();
                System.out.println("somethingagain1[p]");
                for (p = 0; p < 10; p++){
                    System.out.print(something again2[p] + ",");

                }
  1. Extract the common functionality into a new method.

  2. Don't forward declare a for loop variable unless you need it outside the loop

  3. You can use import static to import a static variable of a class for direct use, without the class name. In this case, it's useful for using System.out as out , without having to repeat System.out .

  4. Collapse x instances of System.out.println() into a single out.print("\\n\\n\\n"); call that repeats \\n x times.

Here's my stab at it:

import static java.lang.System.out;

class Main {
    public static void main(String[] args) {
        int[] time = {1, 2, 3};
        int[] something = {1, 2, 3};
        int[] something2 = {1, 2, 3};

        Main.printArray(time);
        out.print("\n\n\n");
        Main.printArray(something);
        out.print("\n\n\n");
        Main.printArray(something2);
    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) out.print(array[i] + ",");
    }
}

First create a method , then take the mutual parameters (array in your situation), inside this method create your for loop, call the method so you don't have to create for loop for each time.

public static void Looper(int[] array) {
    for (int i = 0; i < array.length; i++){
        System.out.print(array[i] + ",");

    }
  }

There are some unclear things to me, but:

0) all this code you should put in main method of some class

1) something again2[p] is wrong syntax, names in Java shouldn't contain spaces!

2) I understand, that all tables you have declared previously ( time , something table);

3) Instead of blank ,,printlines" you should use couple of newline characters instead ( \\n or %n );

4) "somethingagain1[p]" will return you a text, not element of a table!

5) usual letter for iterator is i , but it's up to you what you use :)

6) if you don't use iterator outside loop, you should write something like this:

for (int p = 0; p < 10; p++)

Best regards!

public static void printdata(Object data[] )
{ 
    for (int p = 0; p < data.length; p++)
    {
      System.out.print(data[p] + ",");

    }
}

write above function and call as below.

 printdata(something);

Like Michal mentioned, the naming of your arrays appears incorrect according to Java standards, but let's say you already had three String arrays declared named "one, two, three" already, then it's probably simpler to write your code in a while loop like so:

String[][] arrays = { one, two, three };

int counter = 0;
while( counter < 3){
    for( int p=0; p<10; p++ ){
        System.out.print( arrays[counter][p] + ",");
    }
    System.out.println("\n\n\n");
}

It runs the while until the counter gets to 2, and the for loop is within the while loop. You don't need to declare the int p more than once. Three line returns are sent after the for loop.

It much simpler code, and easy to support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM