Array referencing

Discussion in 'Java' started by anders_tung@hotmail.com, Nov 25, 2004.

  1. Guest

    Hi,

    Here is my routine. It is not working. Anyone see the problem?

    public void test ()
    {

    String[][] data_1 = {
    { "AA_1", "Hello world" },
    { "AA_2", "123456789" }
    };
    String[][] data_2 = {
    { "BB_1", "Bla Bla" },
    { "BB_1", "222222222" },
    };
    String[][] data_3 = {
    { "CC_1", "Happy New Year" },
    { "CC_1", "333333333" },
    };

    for(int i=1; i <=3; i++){
    // following line is wrong. Anyone know how to do it??
    String data_name[][] = "data_"+i;
    System_out_println(" first = "+data_name[j][0];
    System_out_println(" second = "+data_name[j][1];
    }
    }

    Thank you!

    Anders
     
    , Nov 25, 2004
    #1
    1. Advertising

  2. wrote:
    > // following line is wrong. Anyone know how to do it??
    > String data_name[][] = "data_"+i;


    What is it *supposed* to do?

    You're trying to assing a String to a reference of type String[][],
    which is obviously not possible.
     
    Michael Borgwardt, Nov 25, 2004
    #2
    1. Advertising

  3. > Here is my routine. It is not working. Anyone see the problem?
    >
    > public void test ()
    > {
    >
    > String[][] data_1 = {
    > { "AA_1", "Hello world" },
    > { "AA_2", "123456789" }
    > };
    > String[][] data_2 = {
    > { "BB_1", "Bla Bla" },
    > { "BB_1", "222222222" },
    > };
    > String[][] data_3 = {
    > { "CC_1", "Happy New Year" },
    > { "CC_1", "333333333" },
    > };
    >
    > for(int i=1; i <=3; i++){
    > // following line is wrong. Anyone know how to do it??
    > String data_name[][] = "data_"+i;
    > System_out_println(" first = "+data_name[j][0];
    > System_out_println(" second = "+data_name[j][1];
    > }
    > }
    >


    I suppose you are from VisualBasic world...

    What you can do is following:

    Object [] data = new Object[3];
    data[0] = data_1;
    data[1] = data_2;
    data[2] = data_3;

    for(int i=0; i < 3; i++){
    String data_name[][] = (String[][]) data;
    System_out_println(" first = "+data_name[j][0];
    System_out_println(" second = "+data_name[j][1];
    }

    --
    Andrei Kouznetsov
    http://uio.dev.java.net Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
     
    Andrei Kouznetsov, Nov 25, 2004
    #3
  4. "Andrei Kouznetsov" <> schreef in bericht
    news:co5cda$q8p$...
    >> Here is my routine. It is not working. Anyone see the problem?
    >>
    >> public void test ()
    >> {
    >>
    >> String[][] data_1 = {
    >> { "AA_1", "Hello world" },
    >> { "AA_2", "123456789" }
    >> };
    >> String[][] data_2 = {
    >> { "BB_1", "Bla Bla" },
    >> { "BB_1", "222222222" },
    >> };
    >> String[][] data_3 = {
    >> { "CC_1", "Happy New Year" },
    >> { "CC_1", "333333333" },
    >> };
    >>
    >> for(int i=1; i <=3; i++){
    >> // following line is wrong. Anyone know how to do it??
    >> String data_name[][] = "data_"+i;
    >> System_out_println(" first = "+data_name[j][0];
    >> System_out_println(" second = "+data_name[j][1];
    >> }
    >> }
    >>

    >
    > I suppose you are from VisualBasic world...
    >
    > What you can do is following:
    >
    > Object [] data = new Object[3];
    > data[0] = data_1;
    > data[1] = data_2;
    > data[2] = data_3;
    >
    > for(int i=0; i < 3; i++){
    > String data_name[][] = (String[][]) data;
    > System_out_println(" first = "+data_name[j][0];
    > System_out_println(" second = "+data_name[j][1];
    > }


    Please place your brackets consistently, preferably directly behind the
    element type when declaring. That goes for the both of you.
     
    Boudewijn Dijkstra, Nov 25, 2004
    #4
  5. > Please place your brackets consistently, preferably directly behind the
    > element type when declaring. That goes for the both of you.


    I just copy/paste his code, I always put brackets in the middle.

    --
    Andrei Kouznetsov
    http://uio.dev.java.net Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
     
    Andrei Kouznetsov, Nov 25, 2004
    #5
  6. Ricardo Guest

    wrote:
    > Hi,
    >
    > Here is my routine. It is not working. Anyone see the problem?
    >
    > public void test ()
    > {
    >
    > String[][] data_1 = {
    > { "AA_1", "Hello world" },
    > { "AA_2", "123456789" }
    > };
    > String[][] data_2 = {
    > { "BB_1", "Bla Bla" },
    > { "BB_1", "222222222" },
    > };
    > String[][] data_3 = {
    > { "CC_1", "Happy New Year" },
    > { "CC_1", "333333333" },
    > };
    >
    > for(int i=1; i <=3; i++){
    > // following line is wrong. Anyone know how to do it??
    > String data_name[][] = "data_"+i;
    > System_out_println(" first = "+data_name[j][0];
    > System_out_println(" second = "+data_name[j][1];
    > }
    > }


    It seems you want to reference data_1 array when you say "data_"+i.
    You cannot do that. Actually you put an String value(/"data_"+i/) in an
    array, which is wrong.
    What do want to do would be:


    public void test ()
    {
    String[][][] data = {
    {
    {"AA_1", "Hello world"},
    {"AA_2", "123456789"}
    },
    {
    {"BB_1", "Bla Bla"},
    {"BB_1", "222222222"}
    },
    {
    {"CC_1", "Happy New Year"},
    {"CC_1", "333333333"}
    }};


    for(int i=0; i <=1; i++){
    System_out_println(" first = "+data[j][0];
    System_out_println(" second = "+data[j][1];
    }
    }
     
    Ricardo, Nov 25, 2004
    #6
  7. Nigel Wade Guest

    wrote:

    > Hi,
    >
    > Here is my routine. It is not working. Anyone see the problem?
    >
    > public void test ()
    > {
    >
    > String[][] data_1 = {
    > { "AA_1", "Hello world" },
    > { "AA_2", "123456789" }
    > };
    > String[][] data_2 = {
    > { "BB_1", "Bla Bla" },
    > { "BB_1", "222222222" },
    > };
    > String[][] data_3 = {
    > { "CC_1", "Happy New Year" },
    > { "CC_1", "333333333" },
    > };
    >
    > for(int i=1; i <=3; i++){
    > // following line is wrong. Anyone know how to do it??
    > String data_name[][] = "data_"+i;
    > System_out_println(" first = "+data_name[j][0];
    > System_out_println(" second = "+data_name[j][1];
    > }
    > }
    >
    > Thank you!
    >
    > Anders


    Off the top of my head, one way to do this is to use a HashMap:

    HashMap dataMap = new HashMap();
    ....
    dataMap.put("data_1",data_1);
    dataMap.put("data_2",data_2);
    dataMap.put("data_3",data_3);
    for(int i=1; i <=3; i++){
    String[][] data_name = (String[][]) dataMap.get("data_"+i);

    This isn't a general solution to this type of problem, but for this specific
    case it gets the job done, and it's pretty simple.

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, Nov 26, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Asfand Yar Qazi
    Replies:
    7
    Views:
    327
    Asfand Yar Qazi
    Mar 25, 2005
  2. flipflop

    Referencing a global array outside a function

    flipflop, May 8, 2004, in forum: C Programming
    Replies:
    12
    Views:
    11,025
    flipflop
    May 12, 2004
  3. Maarten van Reeuwijk

    Numeric array referencing vs copying.

    Maarten van Reeuwijk, Feb 1, 2004, in forum: Python
    Replies:
    2
    Views:
    359
    John J. Lee
    Feb 2, 2004
  4. peacelittleone

    newbie array referencing (i think)

    peacelittleone, Jan 31, 2005, in forum: C Programming
    Replies:
    13
    Views:
    511
    Richard Bos
    Feb 2, 2005
  5. pinkisntwell
    Replies:
    5
    Views:
    323
    Kenshin
    Dec 17, 2009
Loading...

Share This Page