2D array and coordinate values

Hello there, I'm trying to save inside a 2d array some coordinates of the the processing frame. For instance: I have my array of int positions[][] and I would like to initialize it with my values(75,300,525) (75,300,525). and the scan through them while I need it with a nested loop. In fact the point I need are:75-75,75-30,75-525 and so on.. So for example:

positions[0][0]={{75},{75}}
positions[0][1]={{75},{300}}
positions[0][2]={{75},{525}}

but unfortunately it doesn't work. When I print the values it's completely messed up and I get values like this:[I@1de498 [I@8ae45a

I know it could be done easily using 2 single arrays one for the X and another for the Y. but honestly I woud like to understand better this 2d arrays.

Thank you guys.

Answers

  • positions[0][0]={{75},{75}}

    this is defining an array containing two arrays each containing a single number. what you're seeing when you print it out is a reference to the array objects.

    positions[0][0] = 75; positions[0][1] = 75;

    or

    positions[0] = {75, 75};

    is, i think, what you want (untested)

  • or even

    int[][] positions = {
      {75, 75},
      {75, 300},
      {75, 525}
    };
    

    again, untested

  • edited October 2013

    Thanks guys! I understand what was the problem. I think I was doing right except for a couple of things: While I was testing I used this code to control the flow: println(position[0][0] + " " +i); i was increased by a for cycle. That line doesn't work for some reasons.

    for (int i=0; i<positions.length; i++){
        if(!s){
        println(positions[2][1]);
        print(" ");
        println(i);
         } This for instance does work.. Something I don't understand.
    
    int[][] positions=new int[3][3];
      positions[0][0]={{75},{75}}; //This doesn't work
    
    int[][] positions=new int[3][3];
      positions[0][0]={75};             // This neither
    
    int[][] positions=new int[3][3];
      positions[0][0]={75,75};        //This neither
    
    this insted works fine!
    /*int[][] positions= { {75, 75},
                         {75, 300},
                         {75, 525}
    };*/
    

    and refers accordingly to: 0-0/1
    1-0/1
    2-0/1
    I think that the main problem was that sort of mistake in the println which drove me into the wrong way... thank you guys!

  • edited October 2013

    Besides this at-once format:

    final short[][] positions = {
      {75, 75},
      {75, 300},
      {75, 525}
    };
    
    exit();
    

    We can assign a sub-array 1 by 1 to it: (~~)

    final short[][] positions = new short[3][];
    
    positions[0] = new short[] {75, 75};
    positions[1] = new short[] {75, 300};
    positions[2] = new short[] {75, 525};
    
    exit();
    
Sign In or Register to comment.