简体   繁体   English

如何从 4X4 数组中仅打印 2X2 数组

[英]How to print only 2X2 array from 4X4 array

I am new to Java programming.我是 Java 编程的新手。 I am working with arrays and I just want to print only 2X2 array from 4X4 array.我正在使用 arrays,我只想从 4X4 阵列打印 2X2 阵列。

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).但是,我只想打印3,4,8,7 (右下角 2X2 数组)。 How can I do it?我该怎么做?

This can be accomplished by using normal for loops.这可以通过使用正常的 for 循环来完成。 The collection-based for-each loops that you are using will always iterate through all of the objects in the collection.您正在使用的基于集合的 for-each 循环将始终遍历集合中的所有对象。 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 循环并遍历您关注的索引。

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:让我们有一个 x 和 y 偏移,并打印一个定义的宽度和高度:

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.在此设置中,它将从 2,2 开始打印 2x2,但您可以调整值以在 0,1 处打印 3x3。 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.了解到这一点,我们现在得到 position 或您要打印的数字的索引。

[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 循环解决方案来打印你的问题。

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: 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.在这里,如果我们将二维数组视为表格,则您要查找的索引范围是从row#2 to 3和从column#2 to 3

Note that the index starts from 0 in an array.请注意,索引从数组中的0开始。 Also, a 2-D array is an array of 1-D arrays.此外,二维阵列是一维 arrays 的阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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