如何从2D阵列获得的第一列的内容阵列、内容

2023-09-07 14:01:15 作者:雅痞.

我怎么能拉第一列从我的二维数组?我在我的初始化数组中这样的方法

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]);
            }
        }
    }

它输出

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?

推荐答案

您需要使用调试器,你的IDE和逐步执行code,看看到底是怎么回事。简短的回答我看起来像你重挫相同的字段读取数据两次,K = 0和k = 1。

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.

修改

在你更新另外,我觉得你正在寻找这在你的第二个打印循环(这不是问题,可以很容易地为2,但是这是更好的IMO):

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):

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

修改这里是code我想你要明确:

EDIT Here 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();
            }
        }
    }
}

最后,你的数组初始化错误,这就是为什么你有这么多的麻烦:你实际上是写两个值X成分,所以第一次循环以K你写一个X值,以I,J (例如:数据[0] [0] ),并打印出来。你环路k是第二次你写的Y值,以相同的I,J(例如:数据[0] [0] )再次打印出来。这看起来像要打印你想要的数组,但实际上你正在编写这两个值相同的列。你真的想要的东西没有k循环:

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:

//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;    
}

如果要强制2列,我认为k的做,是这样的:

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

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;    
}

计数也是不必要的,因为它是控制阵列,它已经处理/限于通过循环的每一列(或在第二总大小的只是以不同的方式例如,你知道这是两个宽)由 data.length / 数据[0] .length

count is 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

编辑添加的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.

我不知道为什么我仍坚持这一点,但我认为这是与我如何使用一维数组(无需告诉他们有一个专栏)已。

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).