简体   繁体   中英

How can I loop over the entire array and fetch it?

I have a 2D Array that's data is being fetched from an Enum.

*Enum Params*
int[][] items

And I have a method which accepts 2D Arrays as one of its arguments.

The object is to iterate through all the rows and columns of the 2D Array and fetch its values.

*The Method Im Using Currently*
    public void sendItemsOnInterface(TeleportData data1) {
            resetItemGroups();
            for(int i = 0; i < data1.getItems().length; i++) {
                for(int j = 0; j < data1.getItems()[i].length; j++) {
                    int[][] rewards = {{data1.getItems()[i][0], data1.getItems()[j][1]}};
                player.getPA().sendScrollableItemsOnInterface(28229, rewards);
            }
        }
    }

However its only showing the first part of the array, as in its not showing the entire elements of the array.

The array is listed in this format 0 = Item 1 = Amount.

I'm trying to list all the items and amounts.

I haven't tried you code but, Try this might work

for (int i= 0; i< data1.length; i++) {
        for (int j= 0; j< data1[i].length; j++) {
            data1[i][j] = row * col;
            System.out.print(data1[i][j] + "\t");

Hope this was helpful.

Not sure if this is something you'd wanna do, but if don't need the indexes of the arrays, you should iterate. I'm going crosseyed reading your for loops :)

    for (int[] row: data.getItems()) {
       for (int col: row) {     
              // do your stuff
        }
    }

Elaboration further:

int[][] rewards = {{data1.getItems()[i][0], data1.getItems()[j][1]}}

you are assigning a single row to the rewards array with alternate (i and j) row indexes.... and I hate this web editor! >:( an hour to try to get this written out because I hit backspace and it loses focus and goes back a page on my browser :p

int contents[][] = { {1, 2} , { 4, 5} };

Stole that from another stack overflow post/answer at : Explicitly assigning values to a 2D Array?

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