Results 1 to 14 of 14
  1. #1
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default declaring a different array each time i loop

    Hi,
    I'm writing a program and i'm wondering how i could declare a different array each time i loop through for an unknown number of times. I want the name of each array declared to be one of the values from a different array.

    For example:
    if array[i] = "cars" then i want the name of the the variable declared that time through the loop to be cars.

    Help appreciated.
    Last edited by MichaelT; 06-09-2011 at 01:39 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    loop {
        declare array
        other code
    }
    What don't you understand? Try writing a simple small bit of code to show what you are tring to do.

  3. #3
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    loop {
        declare array
        other code
    }
    What don't you understand? Try writing a simple small bit of code to show what you are tring to do.
    I dont get it because i want a different array to be declared each time through not the same array over and over. Pretty much i want to declare an array that has the name of the value stored in anothervariable
    Last edited by MichaelT; 06-09-2011 at 01:44 AM.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Just to see if I got it right... If the value of a variable is "cars", you want to (codewise) declare a new array called "cars" and be able to use cars.length and cars[0] etc?

  5. #5
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    yea thats it

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You are not explaining yourself. You cannot declare the same array again. Once an array is declared initialised it cannot be changed. If I do:
    Java Code:
    int[] arr;
    while(bool) {
        arr = new arr[10];
    }
    Then a new array will be created each time around the loop. Not the same one.

    Are you saying that you want a loop, gather some information, store it in an array and retain it. Then on the next loop have another array filled with different information. So now you have 2 arrays, etc. If so then create a class that represents the data you want to store. Then you can have an array or List of objects of your class.
    Java Code:
    Foo[] arr = new Foo[10];
    int index = 0;
    while(bool) {
        // get info
        Foo f = new Foo(info);
        arr[index++] = f;
    }

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    It seems I don't have a friggin clue what you are trying to do.

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    What I'm wondering is... Why on earth would you need to do such a thing?

  9. #9
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    heres why i need it. Im reading in data from multiple text files but i dont know how many and i need to store all that information in seperate arrays. One array for each text file. I have no idea to accomplish this because i dont know how i can declare arrays for each text file when i dont know how many text files there are.
    Last edited by MichaelT; 06-09-2011 at 02:00 AM.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    A common problem noobs have is asking how to enabled their perceived solution to a problem. Instead they should describe what the problem is and more experienced people can offer a more suitable solution.

    Contrived analogy:

    Help, my knife doesn't work
    Is it sharp?
    Yes?
    Are you using the blade side
    Yes?
    etc

    Finally it is discovererd that they are trying to cut down a tree. Why not use a chainsaw instead?

  11. #11
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    and so do you have a simpler solution to what im trying to do which i just posted one post above ^.
    Last edited by MichaelT; 06-09-2011 at 02:07 AM.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Please stop editing posts to add more details. I read your post before you added those details and if you had not pointed it out would never read that post again.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    One solution is as I posted above in reply #6. Use a class to store your information.

    Another solution, which is slightly better than using parallel or 2D arrays, is to use a List of arrays.

  14. #14
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Ok thank you the arrayList will work sry about not being clear enough in the OP.

Similar Threads

  1. Checking for two strings in array at the same time
    By veronique in forum New To Java
    Replies: 10
    Last Post: 04-27-2011, 05:36 AM
  2. Array or for loop? or both?
    By kedecr in forum New To Java
    Replies: 4
    Last Post: 03-09-2011, 01:24 PM
  3. Need help declaring array
    By earnest in forum New To Java
    Replies: 7
    Last Post: 02-22-2011, 12:17 AM
  4. Time complexity - foor loop
    By hawaiifiver in forum New To Java
    Replies: 5
    Last Post: 02-05-2011, 04:06 PM
  5. Array for time table
    By blessed07 in forum New To Java
    Replies: 0
    Last Post: 03-06-2010, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •