array of arrays question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: array of arrays question

  1. #1
    jim Guest

    array of arrays question


    Hi,

    I'm trying to load values into an array of arrays. I have a 5x10 array like
    so:
    String[][] words = new String[5][10];

    and i have a method that returns an array of 10 words. so I want to load
    the results of the method into the multidimensional array at [0][x].

    words[0][] = returnWords();

    This doesm't work ("Array index required"), but this does:

    String[] moreWords = new String[10];
    moreWords[] = returnWords();

    any help appreciated!
    jim

  2. #2
    Paul Clapham Guest

    Re: array of arrays question

    And then

    for (int i=0; i<10; i++)
    words[0][i] = moreWords[i];

    PC2

    "jim" <the_nach@hotmail.com> wrote in message
    news:3bb78222$1@news.devx.com...
    >
    > Hi,
    >
    > I'm trying to load values into an array of arrays. I have a 5x10 array

    like
    > so:
    > String[][] words = new String[5][10];
    >
    > and i have a method that returns an array of 10 words. so I want to load
    > the results of the method into the multidimensional array at [0][x].
    >
    > words[0][] = returnWords();
    >
    > This doesm't work ("Array index required"), but this does:
    >
    > String[] moreWords = new String[10];
    > moreWords[] = returnWords();
    >
    > any help appreciated!
    > jim




  3. #3
    jim Guest

    Re: array of arrays question


    Thanks. That's what I ended up using.


    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >And then
    >
    >for (int i=0; i<10; i++)
    > words[0][i] = moreWords[i];
    >
    >PC2
    >
    >"jim" <the_nach@hotmail.com> wrote in message
    >news:3bb78222$1@news.devx.com...
    >>
    >> Hi,
    >>
    >> I'm trying to load values into an array of arrays. I have a 5x10 array

    >like
    >> so:
    >> String[][] words = new String[5][10];
    >>
    >> and i have a method that returns an array of 10 words. so I want to load
    >> the results of the method into the multidimensional array at [0][x].
    >>
    >> words[0][] = returnWords();
    >>
    >> This doesm't work ("Array index required"), but this does:
    >>
    >> String[] moreWords = new String[10];
    >> moreWords[] = returnWords();
    >>
    >> any help appreciated!
    >> jim

    >
    >



  4. #4
    Roy Lachapelle Guest

    Re: array of arrays question


    Jim, You claimed that your were able to do this:
    ...
    String[] moreWords = new String[10];
    moreWords[] = returnWords();
    ...
    I added a returnWords() method like this:

    public String[] returnWords()
    {
    String[] s = {"1","2","3","4","5","6","7","8","9","10"};
    return s;
    }

    and received the following errors during the compile using JDK 1.3.1:

    Test.java:15: not a statement
    moreWords[] = returnWords();
    ^
    Test.java:15: ';' expected
    moreWords[] = returnWords();
    ^

    Even if you are using an older JDK that allows this, you may be replacing
    the reference for your string array, making the 'new String[]'
    initialization useless. In any case, it's dangerous code, and might be
    confusing to maintain.

    You might be better off just writing a routine that gets the size
    of the return array, loops through the entries and assigns them
    (keep that method in a utility class, because you'll use it a lot).
    Alternatively, take a look at the System.arraycopy(...) method (may not
    be exactly what you want, but may make less work of a utility method).

    Last, remember that each 'sub' array can be a different length.
    Take a look at this code:
    ...
    String[][] moreWords = new String[5][]; // notice no secondary length
    moreWords[0] = returnWords(3);
    moreWords[1] = returnWords(10);
    System.out.println("moreWords[0][1] = " + moreWords[0][1]);
    System.out.println("moreWords[1][9] = " + moreWords[1][9]);
    System.out.println("moreWords.length = " + moreWords.length);
    System.out.println("moreWords[0].length = " + moreWords[0].length);
    System.out.println("moreWords[1].length = " + moreWords[1].length);
    ...

    public String[] returnWords(int numWords)
    {
    if (numWords < 0)
    {
    numWords = 0;
    }
    String[] s = new String[numWords];
    for (int i = 0; i < numWords; i++)
    {
    s[i] = "string " + (i+1) + " of " + numWords;
    }
    return s;
    }

    This code produced:

    moreWords[0][1] = string 2 of 3
    moreWords[1][9] = string 10 of 10
    moreWords.length = 5
    moreWords[0].length = 3
    moreWords[1].length = 10

    Attempting to access 'moreWords[0][7]' would result in a
    java.lang.ArrayIndexOutOfBoundsException to be thrown.

    Ok, I know it might not be exactly what you want, but this info should
    point you in the right direction. There are lots of books/articles
    on how arrays work, so you shouldn't have a problem finding more
    information.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center