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

13,721

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

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

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

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

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

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

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

EDIT Added by the 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).

Solution 2

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

Solution 3

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

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

Solution 4

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

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

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

Share:
13,721
Magpie
Author by

Magpie

1129CMrZkqKt32F5kJhYiNMsfhRnWFEeTA Please answer GNOME Shell magnifier user feedback questionairre

Updated on July 28, 2022

Comments

  • Magpie
    Magpie almost 2 years

    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?

  • Magpie
    Magpie over 11 years
    Sorry I don't follow. I ran analyse on intellij and nothing obvious.
  • Magpie
    Magpie over 11 years
    Oh I think I get what you mean now. You mean I am writing over the first column with with second column?
  • Matthew
    Matthew over 11 years
    Yes - you write to data[i][j] twice without changing i or j, as you loop through k twice creating your printout.
  • Matthew
    Matthew over 11 years
    I suspect we were both downvoted strategically for the bounty.
  • Magpie
    Magpie over 11 years
    It wasn't me. That answer didn't work on my problem though for the record. I am going to check these other ones just now.
  • Magpie
    Magpie over 11 years
    I had put the contents of the input file on the post. I will edit to make that clearer though because I can see how it might not have been.
  • Magpie
    Magpie over 11 years
    Also to answer your question I am tying to read the first and second values and then do a new line.
  • jlordo
    jlordo over 11 years
    We could help you better, if you show us what IOUtil.skipToDouble(Scanner) does
  • Magpie
    Magpie over 11 years
    I am a bit confused as to how you've named the methods. Was there a motivation for calling them call printX or is that a copy-paste error?
  • Magpie
    Magpie over 11 years
    It skips over anything that isn't a numeric value which is useful if you have a file with labels but not needed for this particular file really!
  • Magpie
    Magpie over 11 years
    I got it working and I completely get it now and my fear of 2D arrays has been cured. Thanks, but that said, I would like to make sure there is a really complete answer while the bounty is active because I think it is something that might help other people who are struggling with the concept of 2D arrays too (and it'll be years before I get that 50 back ;)). Although all the answers helped collectively, most of all yours. Not one of them really seems to recognise what I had failed to understand about the way two dimensional arrays work (in java, and probably the other languages like c++ too);
  • Matthew
    Matthew over 11 years
    @Magpie Glad I could help. If you care to suggest an edit I would be happy to add it to the post - Perhaps a link to a resource such as this will help: leepoint.net/notes-java/data/arrays/arrays-2D.html
  • Magpie
    Magpie over 11 years
    It seems so obvious to me now I can't believe I didn't see it but I think your suggestion is sensible. I won't get a chance to give it brain space till the weekend at the earliest though so I will accept this answer as it is till then. +1 for all those who helped. Much appreciated.