Java的多维数组 [英] Java Multidimensional Arrays

查看:173
本文介绍了Java的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习Java的过程,是对多维数组很困惑​​。当我说这我并不意味着数组语法但更多的使用数组与语句的逻辑。我想知道是怎么做的我正确的,什么是都在玩code做集成阵列成报表,为什么是在那里。下面是一些code我一直在努力(基于关闭教程),并想知道如果有人能完全解释是怎么回事的一切。

 包教程;公共类苹果{
公共静态无效塔科(字串[] args){
    INT firstarray [] [] = {{8,9,10,11},{12,13,14,15}};
    INT secondarray [] [] = {{30,31,32,33},{43},{4,5,6}};    的System.out.println(这是第一阵列);
    显示器(firstarray);    的System.out.println(这是第二阵列);
    显示器(secondarray);
}公共静态无效显示(INT X [] []){
    对于(INT行= 0;&行LT; x.length;排++){
        对于(INT列= 0;&列LT; X [行]。长度;柱++){
            的System.out.println(X [行] [列] +\\ t的);
        }
        的System.out.println();
    }
}

所以我不明白的是整个

 公共静态无效显示(INT X [] []){
    对于(INT行= 0;&行LT; x.length;排++){
        对于(INT列= 0;&列LT; X [行]。长度;柱++){
            的System.out.println(X [行] [列] +\\ t的);

如果有人可以解释,在更深入的将是巨大的。我得到了在一般情况下,我只是用它如何工作困惑的语句和数组。


解决方案

在声明数组是这样的:

  INT ARR [] [] = {{8,9,10,11},{12,13,14,15}};

您是pretty多声明2行4列的阵列。视觉上是有意义的认为它是这样的:

{} 8,9,10,11

{} 12,13,14,15

然后,当您试图访问某个元素,觉得作为顶级最左边的元素[0] [0]。

所以ARR [0] [0]就等于8.

我发现最容易想到的多维阵列的不只是作为行/列,也可作为x和y。除了这是一个有点棘手,因为它是反直觉的意义格式为:

改编[y_coordinate] [x_coordinate]。

,你从8向右移动,x坐标增加而增加。

当你从8向下移动,y坐标增加。

所以,进入9,你可以使用ARR [0] [1]。要访问12,你可以使用ARR [1] [0]。

现在就交给你想要的方式解释说:

 公共静态无效显示(INT X [] []){
    对于(INT行= 0;&行LT; x.length;排++){
        对于(INT列= 0;&列LT; X [行]。长度;柱++){
            的System.out.println(X [行] [列] +\\ t的);

x.length返回行的y坐标的数目,或者,你的多-D阵列具有

×〔行]。长度返回列数,或x坐标,特定行索引具有。

这是什么方法确实是它从顶端开始,y坐标(第一行),然后检查/ X多少列坐标该行了。然后,它通过该行中中的x坐标循环从左至右打印其值。当它已完成打印行中的所有的值,它移动到下一个行<!/ P>

在一个侧面说明,我会非常轻微编辑您的显示方法:

 公共静态无效显示(INT X [] []){
    对于(INT行= 0;&行LT; x.length;排++){
        对于(INT列= 0;&列LT; X [行]。长度;柱++){
            System.out.print(X [行] [列] +\\ t的);
        }
        的System.out.println();
    }
}

我改变了的System.out.println以System.out.print

为什么?因为那时只打印下一行的新行。

让我知道如果这任何部分还不清楚。

I am in the process of learning Java and am very confused on multidimensional arrays. When I say this I don't mean the array syntax but more so the logic of using arrays with for statements. What I am wondering is how do I incorporate arrays into for statements correctly and what does all of the code in play do, and why is it there. Here is some code I have been working on (based off a tutorial) and was wondering if someone could fully explain everything that is going on.

package tutorial;

public class apples {
public static void taco(String[] args) {
    int firstarray[][]={{8,9,10,11},{12,13,14,15}};
    int secondarray[][]={{30,31,32,33},{43},{4,5,6}};

    System.out.println("This is the first array");
    display(firstarray);

    System.out.println("This is the second array");
    display(secondarray);
}

public static void display(int x[][]) {
    for (int row=0;row<x.length;row++) {
        for (int column=0;column<x[row].length;column++) {
            System.out.println(x[row][column]+"\t");
        }
        System.out.println();
    }
}

So what I don't understand is the entire

public static void display(int x[][]) {
    for (int row=0;row<x.length;row++) {
        for (int column=0;column<x[row].length;column++) {
            System.out.println(x[row][column]+"\t");

If someone could explain that in more depth that would be great. I get for statements and arrays in general, im just confused with how this works.

解决方案

When you declare an array like this:

int arr[][]={{8,9,10,11},{12,13,14,15}};    

You're pretty much declaring an array with 2 rows and 4 columns. Visually it makes sense to think of it like this:

{8,9,10,11}

{12,13,14,15}

Then when you try to access a certain element, think of the top leftmost element as [0][0].

So arr[0][0] would equal 8.

I find it easiest to think of multi dimensional arrays not just as rows/columns, but also as x and y. Except it's a little tricky because it's counter-intuitive in the sense the format is:

arr[y_coordinate][x_coordinate].

as you move from 8 to the right, the x coordinate increases.

as you move from 8 downwards, the y coordinate increases.

So, to access 9, you could use arr[0][1]. To access 12, you could use arr[1][0].

Now on to the method you'd like explained:

public static void display(int x[][]) {
    for (int row=0;row<x.length;row++) {
        for (int column=0;column<x[row].length;column++) {
            System.out.println(x[row][column]+"\t");

x.length returns the number of rows, or y coordinates, your multi-d array has.

x[row].length returns the number of columns, or x coordinates, a particular row index has.

What this method does is it starts at the top y coordinate (the first row), and then it checks how many columns/x coordinates that row has. It then loops through those x coordinates in that row from left to right printing its value. When it has finished printing all the values in a row, it moves on to the next row!

On a side note, I would edit your display method ever so slightly to:

 public static void display(int x[][]) {
    for (int row=0;row<x.length;row++) {
        for (int column=0;column<x[row].length;column++) {
            System.out.print(x[row][column]+"\t");
        }
        System.out.println();
    }
}

I changed a System.out.println to System.out.print

why? Because then it only prints a new line for the next row.

Let me know if any part of this is unclear.

这篇关于Java的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆