1

I met a problem. There is a 2d array include 12 cities' trade volume in each month. The cities are listed in a string array. I'm trying to make a table to list the city trade records each month.I tried to use a nested 3 -times for loop but the cities name always in the circulation for several times,it only need once to appear in the first colunm of each city. Can someone tells me where is my problem? I'm out of mind.

I can't add pictures but I can tell you the program keep generate city names in each first column. I think I should not put the city array in the for loop. But how can I add the city name to each row only once?

int[][] sold = 
  { 
     { 190, 180, 435, 110, 223, 130, 123, 103, 135, 300, 392, 324 },
     { 520, 737, 264, 822, 562, 645, 205, 447, 126, 214, 2784 2138 }, 
     { 1103, 2203, 2376, 3038, 1721, 2463, 3541, 1183, 1213, 3310, 1416, 1523 }, 
     { 2110, 2630, 234, 108, 1439, 202, 2163, 538, 2414, 1155, 167, 2121 },
     { 2031, 274, 2236, 1823, 152, 107, 1912, 265, 123, 1130, 146, 1152 },
     { 2233, 1814, 2316, 2334, 1167, 1231, 2048, 2011, 1486, 1109, 1441, 1176 },
     { 1361, 2119, 1313, 1911, 1873, 2011, 2783, 1046, 1513, 1732, 1109, 1416 },
     { 2011, 1043, 1221, 131, 1231, 169, 2146, 1001, 123, 161, 619, 175 },
     { 1010, 2345, 106, 222, 175, 143, 2018, 1517, 163, 141, 208, 189 }
  };
    


  String[] cities= { 
     "Miami", 
     "Perth", 
     "Sydney",
     "Tokyo", 
     "Mosco", 
     "Winnipeg",
 "London",
     "Beijing", 
     "Guelph"
  };

Thanks a lot

And I write code like this

   for(int city = 0;city<cities.length;city++) {
  
    System.out.printf("%s\n", cities[city]);
    for(int i = 0; i<sold.length;i++) {
        for(int j =0;j<sold[i].length;j++) {
            System.out.printf("%15d",sold[i][j]);
        }
        
    }System.out.println();
}
1
  • Could you include what the console is returning in your question? Commented Jan 20, 2022 at 22:50

1 Answer 1

3

So, assuming that there is a one-to-one relationship of the cities and the sold data, you can use the outer loop as the indexer for the cities and then pull the data from sold for it in the inner loop, for example

public class Test {

    public static void main(String[] args) {
        new Test();
    }
    int[][] sold
            = {
                {190, 180, 435, 110, 223, 130, 123, 103, 135, 300, 392, 324},
                {520, 737, 264, 822, 562, 645, 205, 447, 126, 214, 2784, 2138},
                {1103, 2203, 2376, 3038, 1721, 2463, 3541, 1183, 1213, 3310, 1416, 1523},
                {2110, 2630, 234, 108, 1439, 202, 2163, 538, 2414, 1155, 167, 2121},
                {2031, 274, 2236, 1823, 152, 107, 1912, 265, 123, 1130, 146, 1152},
                {2233, 1814, 2316, 2334, 1167, 1231, 2048, 2011, 1486, 1109, 1441, 1176},
                {1361, 2119, 1313, 1911, 1873, 2011, 2783, 1046, 1513, 1732, 1109, 1416},
                {2011, 1043, 1221, 131, 1231, 169, 2146, 1001, 123, 161, 619, 175},
                {1010, 2345, 106, 222, 175, 143, 2018, 1517, 163, 141, 208, 189}
            };

    String[] cities = {
        "Miami",
        "Perth",
        "Sydney",
        "Tokyo",
        "Mosco",
        "Winnipeg",
        "London",
        "Beijing",
        "Guelph"
    };

    public Test() {
        for (int cityIndex = 0; cityIndex < cities.length; cityIndex++) {
            System.out.printf("%-8s", cities[cityIndex]);
            for (int data : sold[cityIndex]) {
                System.out.printf("%15d", data);
            }
            System.out.println("");
        }
    }
}

Which prints...

Miami               190            180            435            110            223            130            123            103            135            300            392            324
Perth               520            737            264            822            562            645            205            447            126            214           2784           2138
Sydney             1103           2203           2376           3038           1721           2463           3541           1183           1213           3310           1416           1523
Tokyo              2110           2630            234            108           1439            202           2163            538           2414           1155            167           2121
Mosco              2031            274           2236           1823            152            107           1912            265            123           1130            146           1152
Winnipeg           2233           1814           2316           2334           1167           1231           2048           2011           1486           1109           1441           1176
London             1361           2119           1313           1911           1873           2011           2783           1046           1513           1732           1109           1416
Beijing            2011           1043           1221            131           1231            169           2146           1001            123            161            619            175
Guelph             1010           2345            106            222            175            143           2018           1517            163            141            208            189
Sign up to request clarification or add additional context in comments.

2 Comments

May I ask what is for (int data : sold[cityIndex]) mean,Is there a complex style for this code?
It's an enhanced for-each loop, which will take each item from the sequence (in this the array) and make it available on each subsequent loop, think of it as a short handed approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.