简体   繁体   中英

Print two arrays on same line

I'm sure this will be a simple question but I can't seem to find the right answer. I have two distinct String arrays, each with data and each have exactly the same array length (21). I'm simply trying to loop through both indexes and print index value 0 for array 1 and index value 0 for array 2 on the same line and so forth.

Here's what I have. I have one array called weightToOunce and another called weightArray. The problem with this code is it's based on the length of both arrays so each index value within the array is printed the length of the array (21 times in this case).

I've tried moving the print statement out of the loop and declaring an int i and int j variable before the loop but I haven't been successful there either.

public static void weightArrayLooper() {
    String[] weightToOunce = Project1.setWeightPercentageToOunce();
    for (int i = 0;i<weightArray.length;i++) {
        for (int j = 0;i<weightToOunce.length;j++) {
            System.out.println("Lb " + weightArray[i] +  " Oz " + weightToOunce[j]);
        }
    }
}

Not sure if this is what you are trying to do, but you can use the same 'i' value to traverse both array!

public static void weightArrayLooper() {
    String[] weightToOunce = Project1.setWeightPercentageToOunce();
    for (int i = 0;i < weightArray.length;i++) {
            System.out.println("Lb " + weightArray[i] +  " Oz " + weightToOunce[i]);
        }
}

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