简体   繁体   中英

How to print only 2X2 array from 4X4 array

I am new to Java programming. I am working with arrays and I just want to print only 2X2 array from 4X4 array.

Example:

int a[][]= {{2,4,6,8},
            {1,3,5,7},
            {1,2,3,4},
            {0,9,8,7}};

Using the below code I am able to print all the numbers in an array.

for(int i[]:a) {
    for(int j:i) {
        System.out.println(j + "");
    }
}

But, I just want to print only 3,4,8,7 (bottom right 2X2 array). How can I do it?

This can be accomplished by using normal for loops. The collection-based for-each loops that you are using will always iterate through all of the objects in the collection. If you want to iterate over only a certain range of an array, use a normal for loop and iterate over the indexes you are concerned with.

for (int i = 2; i < 4; i++) {
    for (int j = 2; j < 4; j++) {
        System.out.println("" + a[i][j]);
    }
}

Let's have an x and y offset, and print a defined width and height:

int offset_x = 2;
int offset_y = 2;
int width = 2;
int height = 2;

for (int y = offset_y; y >= 0 &&  y < offset_y + height && y < a.length; y++) {
    for (int x = offset_x; x >= 0 && x < offset_x + width && x < a[y].length; x++) {
        System.out.print(a[y][x]+",");
    }
    System.out.println();
}

In this setup it will print the 2x2 starting at 2,2 but you could tweak the values to print a 3x3 at 0,1 for example. It's protected from running off the end of an array, and it won't print if negative indexes are used

You can visualize

2 4 6 8
1 3 5 7
1 2 3 4
0 9 8 7

these set of numbers as

[0,0] [0,1] [0,2] [0,3]
[1,0] [1,1] [1,2] [1,3]
[2,0] [2,1] [2,2] [2,3]
[3,0] [3,1] [3,2] [3,3]

The 1st number in the bracket represents the row, while the 2nd number represent the column.

Learning that, we now get the position or indexes of the numbers you want to print.

[2,2] [2,3]
[3,2] [3,3]

From your given set of numbers, I get their row and column indexes then analyze a better for loop solution to print your problem.

for (int row = 2; row < 4; row++) {
    for (int col = 2; col < 4; col++) {
          System.out.println(a[row][col]) ;
    } 
}

The solution above is no different than this:

System.out.println(a[2][2]);
System.out.println(a[2][3]);
System.out.println(a[3][2]);
System.out.println(a[3][3]);

In order to print elements from a range of indices, you need to use nested loops to navigate through the indices in the range. Do it as follows:

public class Main {
    public static void main(String[] args) {
        int a[][] = { 
                { 2, 4, 6, 8 }, 
                { 1, 3, 5, 7 }, 
                { 1, 2, 3, 4 }, 
                { 0, 9, 8, 7 } };
        for (int i = a.length / 2; i < a.length; i++) {
            for (int j = a[i].length / 2; j < a[i].length; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Output:

3 4 
8 7 

Here, the range of indices you are looking for is from row#2 to 3 and from column#2 to 3 if we think of a 2-D array as a table.

Note that the index starts from 0 in an array. Also, a 2-D array is an array of 1-D arrays.

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