java 如何从二维数组中获取第一列的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14512407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:30:12  来源:igfitidea点击:

How to get the first column's contents from a 2D array

javaarrays2d

提问by Magpie

How can I pull the first column from my 2D array? I am initialising my array in a method like this

如何从二维数组中提取第一列?我正在用这样的方法初始化我的数组

//Initialise the array with the values from the file
public static float[][] data(float[][] data, Scanner scan){
    int count = 0; 

    for (int i=0;i<data.length;i++){
        for (int j=0;j<data[0].length;j++){
            count++;    
            if(count<data.length*data[0].length){
                for(int k=0;k<2; k++){

                    data[i][j] = (float)   IOUtil.skipToDouble(scan); 
                    System.out.print(data[i][j] + "  ");    
                }

            System.out.println();
            }   
        }

    }
    return data;    

}

This is the contents of my test file. (Please bare in mind the file could be any length)

这是我的测试文件的内容。(请记住文件可以是任何长度)

13.97  2.2  
12.05  1.9  
9.99  1.5  
8.0  1.3  
6.0  0.9  
4.0  0.6  
2.0  0.3  
0.0  0.0  
-2.0  -0.3  
-4.0  -0.6  
-6.02  -0.9  
-8.0  -1.3  
-9.99  -1.6  
-12.03  -1.9  
-13.98  -2.2

on the terminal but the problem is when I try to call the left column I am getting the right column. I am not sure if this is because the values on the right are the only ones getting stored or not.

在终端上,但问题是当我尝试调用左列时,我得到的是右列。我不确定这是不是因为右边的值是唯一被存储的值。

      //should print x but actually prints y 
   public static void printX(float[][] data){

        for (int i=0;i<data.length;i++){
            for (int j=0;j<data[0].length;j++){

                System.out.println(data[i][j]);
            }
        }
    }

which outputs

哪个输出

2.2
1.9
1.5
1.3
0.9
0.6
0.3
0.0
-0.3
-0.6
-0.9
-1.3
-1.6
-1.9
-2.2
0.0

Can anyone clarify how I could best get the data on the left hand column from the 2D array ? Or if there is some other method for achieving this sort of thing?

谁能澄清我如何最好地从 2D 数组中获取左侧列的数据?或者是否有其他方法可以实现这种事情?

回答by Matthew

You need to use the debugger on you IDE and step through your code to see exactly what is going on. The short answer to me looks like you are clobbering the same field with read data twice for k=0 and k=1.

您需要在 IDE 上使用调试器并逐步执行代码以准确了解发生了什么。对我来说,简短的回答看起来像你用 k=0 和 k=1 的两次读取数据破坏了同一个字段。

EDIT

编辑

Also in your update, I think you are looking for this in your second print loop (not that it matters, could just as easily be 2, but this is better IMO):

同样在您的更新中,我认为您正在第二个打印循环中寻找这个(并不重要,可以很容易地为 2,但这是更好的 IMO):

for (int j=0;j<data[i].length;j++){

EDITHere is the code I think you want explicitly:

编辑这是我认为您明确想要的代码:

//Only print X
public static void printX(float[][] data){

    for (int i=0;i<data.length;i++){
       System.out.println(data[i][0]);
    }
}

//Only print Y
public static void printY(float[][] data){
    for (int i=0;i<data.length;i++){
       System.out.println(data[i][1]);
    }
}

//Print both
public static void printBoth(float[][] data){
    for (int i=0;i<data.length;i++){
       System.out.println(data[i][0] + ", " + data[i][1]);
    }
}


//Print any number in array:
public static void printAll(float[][] data){
    for (int i=0;i<data.length;i++){
        for (int j=0;j<data[i].length;j++){
            System.out.print(data[i][j];
            if (j+1 < data[i].length) {
                System.out.print(", ");
            } else {
                System.out.println();
            }
        }
    }
}

Finally, Your array is initializing incorrectly, which is why you are having so much trouble: You are actually writing both values to the X component, so the first time you loop in k you write a the X value to i,j (eg data[0][0]) and print it out. The second time you loop k you write the Y value to the same i,j (eg data[0][0]) again and print it out. This looks like you are printing the array you want, but actually you are writing both values to the same column. You really want something without the k loop:

最后,您的数组初始化不正确,这就是您遇到这么多麻烦的原因:您实际上是将两个值都写入 X 组件,因此第一次在 k 中循环时,您将 X 值写入 i,j(例如data[0][0])并打印出来。第二次循环 k 时,您data[0][0]再次将 Y 值写入相同的 i,j (例如)并打印出来。看起来您正在打印所需的数组,但实际上您正在将两个值写入同一列。你真的想要没有 k 循环的东西:

//Initialise the array with the values from the file
public static float[][] data(float[][] data, Scanner scan){
    int count = 0; 

    for (int i=0;i<data.length;i++){
        for (int j=0;j<data[0].length;j++){
            count++;    
            if(count<data.length*data[0].length){
                data[i][j] = (float)   IOUtil.skipToDouble(scan); 
                System.out.print(data[i][j] + "  ");
            }    
        }
        System.out.println();
    }
    return data;    
}

If you want to enforce 2 columns which I think k is doing, something like:

如果您想强制执行我认为 k 正在做的 2 列,例如:

public static float[][] data(float[][] data, Scanner scan){
    for (int i=0;i<data.length;i++){
        for (int j=0;j<2;j++){
            data[i][j] = (float)   IOUtil.skipToDouble(scan); 
            System.out.print(data[i][j] + "  ");
        }
        System.out.println();
    }
    return data;    
}

countis also unnecessary as it is just a different way of controlling the total size of the array which it already handled / limited by looping each column (or in the second example where you know it is two wide) by the data.length/ data[0].length

count也是不必要的,因为它只是一种控制数组总大小的不同方式,它已通过循环每列(或在第二个示例中,您知道它是两列宽)通过data.length/data[0].length

EDIT Added by the OP:

由 OP 添加的编辑:

To clarify for anyone who still does not get it who maybe is confused about the same thing I was: I misunderstood where the values were being stored so this is why I set the problem up wrong and none of the short answers were making any sense.

向那些仍然不明白的人澄清一下,他们可能对我所遇到的同样事情感到困惑:我误解了值的存储位置,所以这就是为什么我将问题设置错误并且没有任何简短答案有意义的原因。

I took the whole thing a bit too literally, assuming that the value[here][] was holding the values from the first column and the value[][here] was holding the values from the second column.

我把整个事情从字面上理解了一点,假设 value[here][] 保存了第一列中的值,而 value[][here] 保存了第二列中的值。

I am not sure why I held onto this, but I think it has something to do with how I had been using 1D arrays (without needing to tell them to have a column).

我不确定我为什么坚持这一点,但我认为这与我一直使用一维数组的方式有关(不需要告诉他们有一个列)。

回答by jlordo

Don't understand your code, but I hope this helps:

不明白你的代码,但我希望这会有所帮助:

float[][] data = new float[10][2];
// ... fill the array with data
// print 'left' column:
for (int i = 0; i < data.length; i++) {
    System.out.println(data[i][0]);
}

回答by jlordo

What are you trying to do with this part of your code?

你想用这部分代码做什么?

for(int k=0;k<2; k++){
    data[i][j] = (float)   IOUtil.skipToDouble(scan); 
    System.out.print(data[i][j] + "  ");    
}

You are writing into data[i][j]twice, and thus overwrite the first value returned by

您正在写入data[i][j]两次,因此覆盖了返回的第一个值

(float) IOUtil.skipToDouble(scan);

It's hard to tell what the code should be without seeing your input file, but I think you are looking for something like this:

如果没有看到您的输入文件,很难说出代码应该是什么,但我认为您正在寻找这样的东西:

public static float[][] data(float[][] data, Scanner scan){
    for (int i = 0; i < data.length; i++) {
        for (int j = 0; j < data[i].length; j++) {
            data[i][j] = (float) IOUtil.skipToDouble(scan); 
            System.out.print(data[i][j] + "  ");    
        }
        System.out.println();
    }
    return data;
}

回答by Vishal K

While putting data in 2D array you are overwriting the content of array twice within the for loop for(int k=0;k<2; k++). For example:consider the case when i = 0:

  1. At k = 0, data[0][0] = 13.97
  2. At k = 1, data[0][0] = 2.2 (Overwrites 13.97) Whereas, the desired result that you are looking for is data[0][0] = 13.97 and data[0][1]=2.2

将数据放入二维数组时,您将在 for 循环中两次覆盖数组的内容for(int k=0;k<2; k++)。例如:考虑 i = 0 的情况:

  1. 在 k = 0 时,数据 [0][0] = 13.97
  2. 在 k = 1 时,data[0][0] = 2.2(覆盖 13.97)而您正在寻找的期望结果是 data[0][0] = 13.97 和 data[0][1]=2.2

You should use the following code to fill the 2D array:

您应该使用以下代码来填充二维数组:

public static float[][] data(float[][] data, Scanner scan){
    int count = 0; 

    for (int i=0;i<data.length;i++){
        for (int j=0;j<data[0].length;j++){
            count++;    
            if(count<data.length*data[0].length){
                for(int k=0;k<2; k++){

                    data[i][j] = (float)   IOUtil.skipToDouble(scan); 
                    System.out.print(data[i][j] + "  ");    
                    j = j+1;
                }

            System.out.println();
            }   
        }

    }
    return data;    

}

And following Code to read the data:

以及以下代码来读取数据:

public static void printX(float[][] data)
{
    for (int i=0;i<data.length;i++)
    {
        System.out.println(data[i][0]);
    }
}

AlsoIf condition if(count<data.length*data[0].length)fails for some value of ithen in that case you will have data[i][0] = 0 and data[i][1] = 0 . You should also consider this fact and should expect 0as some of the outputs by printX .

此外,如果条件if(count<data.length*data[0].length)对某些值失败,i那么在这种情况下,您将拥有 data[i][0] = 0 和 data[i][1] = 0 。您还应该考虑这个事实,并且应该期待 0printX 的一些输出。