Results 1 to 14 of 14
- 06-09-2011, 01:33 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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.
- 06-09-2011, 01:38 AM #2
What don't you understand? Try writing a simple small bit of code to show what you are tring to do.Java Code:loop { declare array other code }
- 06-09-2011, 01:42 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
- 06-09-2011, 01:46 AM #4
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?
- 06-09-2011, 01:49 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
yea thats it
- 06-09-2011, 01:50 AM #6
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:
Then a new array will be created each time around the loop. Not the same one.Java Code:int[] arr; while(bool) { arr = new arr[10]; }
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; }
- 06-09-2011, 01:52 AM #7
It seems I don't have a friggin clue what you are trying to do.
- 06-09-2011, 01:53 AM #8
What I'm wondering is... Why on earth would you need to do such a thing?
- 06-09-2011, 01:54 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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.
- 06-09-2011, 01:59 AM #10
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?
- 06-09-2011, 02:01 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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.
- 06-09-2011, 02:06 AM #12
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.
- 06-09-2011, 02:09 AM #13
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.
- 06-09-2011, 02:13 AM #14
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Checking for two strings in array at the same time
By veronique in forum New To JavaReplies: 10Last Post: 04-27-2011, 05:36 AM -
Array or for loop? or both?
By kedecr in forum New To JavaReplies: 4Last Post: 03-09-2011, 01:24 PM -
Need help declaring array
By earnest in forum New To JavaReplies: 7Last Post: 02-22-2011, 12:17 AM -
Time complexity - foor loop
By hawaiifiver in forum New To JavaReplies: 5Last Post: 02-05-2011, 04:06 PM -
Array for time table
By blessed07 in forum New To JavaReplies: 0Last Post: 03-06-2010, 03:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks