Dynamically populating an array of indefinite size

Hi,

I have a problem that's been stumping me for awhile.

My Java application will read a file, and extract a number of strings from it. The number of strings varies; there could be one string, there could be fifteen.

What I am trying to do is put all of those strings into an array that is dynamically created.

Now, the application will know how many strings there are, so it can know how many elements to put in the array index or whatever. Let me give an example. This is hard-coded, as I have not completed part of this application yet. Be sure to keep in mind however, that the number of strings will change all the time. This example uses four strings:

staticint myFileVars = 4;

static String fileVar1 ="csl_group";

static String fileVar2 ="csl_calname";

static String fileVar3 ="csl_owner";

static String fileVar4 ="cdl_status";

I have been banging my head against the wall trying to figure out how to put these strings into an array. It would be easy if the number of strings did not change all the time. However, that is not the case.

I need some way for my application to create an array that will put all of these strings into it, whether there are four strings or nine.

I've tried using an ArrayList, but I run into the same problem...how do I put each of those elements into an array? This is driving me nuts.

Is there a better way I should be doing this, perhaps?

Thanks for any help you can give,

Dan

Message was edited by:

Djaunl

[1860 byte] By [Djaunla] at [2007-10-3 2:48:05]
# 1
I dont understand you. Why dont you just use an ArrayList?
TuringPesta at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 2
> I dont understand you. Why dont you just use an> ArrayList?How would I dynamically populate (i.e. at runtime) the ArrayList based on the number of strings I have?
Djaunla at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 3

Does the program know the number of Strings in the file BEFORE

it reads the Strings?

if so:

int numStrs = myFile.getTheNumberOfStrings();

String[] strings = new String[numStrs]

for(int i = 0; i < numStrs; i++){

strings[ i] = myFile.getTheNextString();

}

otherwise:

ArrayList stringList = new ArrayList();

stringList.add( myFile.getTheNextString() );

you can also turn a List into an Array (which would be stupid):

String[] strings = stringList.toArray();

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html

TuringPesta at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 4

> How would I dynamically populate (i.e. at runtime) the ArrayList based on the number of strings I have?

You are confused. You dont predetermine the size of the ArrayList. You just add things to it.

ArrayList.add( myString );

You can do that as much as you want until you run out of memory.

TuringPesta at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 5

No, I know that. What I'm saying is, I can't just code in:

Arraylist.add(fileVar1);

Arraylist.add(fileVar2);

Arraylist.add(fileVar3);

Arraylist.add(fileVar4);

I can't do that because there might be five fileVar variables. Or there might be one. That's what is bothering me. If I just add the above code, and my Java application retrieves five variables from the file, the fifth one won't be added.

I guess what I am saying is: I know the size of an ArrayList does not need to be set. However, the application must know how many Strings to add to it, and the number of Strings changes each time.

Message was edited by:

Djaunl

Djaunla at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 6

Man, you are cuckoo bananas.

You either need to step away to clear your mind or you need to

start learning Java from the beginning and brush up on OOP.

public void MyOOPMethodToReadFromMyFile(){

some kind of loop {

String string = myFile.getNextStringSomehow();

addStringToMyCollectionOfStrings(string);

} check for EOF or something else to break loop

}

public void addStringToMyCollectionOfStrings(String string){

myArrayList.add(string);

}

TuringPesta at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 7

> No, I know that. What I'm saying is, I can't just

> code in:

>

> Arraylist.add(fileVar1);

> Arraylist.add(fileVar2);

> Arraylist.add(fileVar3);

> Arraylist.add(fileVar4);

>

> I can't do that because there might be five fileVar

> variables. Or there might be one. That's what is

> bothering me. If I just add the above code, and my

> Java application retrieves five variables from the

> file, the fifth one won't be added.

I think you might be confused on how to get the Strings out of the file - how did you plan to populate fileVar1, fileVar2, etc.?

Lets assume your file has one logical string per record: read each record (using an InputStreamReader, or such) and as you read the record put its contend into the ArrayList (using the add() method).

Or maybe I am not understanding your problem.

jbisha at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 8
Hi, It is really a good one. I have also faced the same problem. I have one solution. Actually U just need to create the object at runtime.I am very sorry to say that I have missed that Web Document :-(, Anyway I have found that document by searching dynamic object creation at
DeepeshDeomuraria at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 9
Youre crazier than the other guy! : )
TuringPesta at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 10
CROSS POST http://forum.java.sun.com/thread.jspa?messageID=4341755
TuringPesta at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 11

> Hi,

> It is really a good one. I have also faced the same

> problem. I have one solution. Actually U just need

> to create the object at runtime.I am very sorry to

> say that I have missed that Web Document :-(, Anyway

> I have found that document by searching dynamic

> object creation at RunTime.

O_o

jbisha at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 12

> Hi,

> It is really a good one. I have also faced the same

> problem. I have one solution. Actually U just need

> to create the object at runtime.I am very sorry to

> say that I have missed that Web Document :-(, Anyway

> I have found that document by searching dynamic

> object creation at RunTime.

The blind leading the blind...

%

duffymoa at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 13
I sense a disturbance in the sanity ...err.. force.The recipe is very simple:- open your file- read one line after another, until you have reached the end- everytime, read the line into a String, and add it to your ArrayList
Mongera at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 14

> CROSS POST

> http://forum.java.sun.com/thread.jspa?messageID=434175

That was a different issue. That was more of a newbish question, so I posted it in the "New to Java" forum. This is more of a programming question, and it is different.

I'm not in such a big need for advice that I would post the same question on two different boards.

As you said in the other thread:

>Djaunl, What is wrong with you? We were JUST helping you a couple >minutes ago. There was no need to repeat your question somewhere >else... it is rude. People just repeated the answers

>you were just given.

I did not repeat the question. Or if you believe it is the same question, I apologize, I did not intend to repeat the same question. In the other thread, I was sort of asking if what I wanted was possible. In this one, I'm wondering how to do it. As you may have noticed, I'm "cuckoo bananas", so it's not that easy for me to tell the differences or similarities between concepts. If I repeated the question, I apologize; I will try to be more careful in the future.

>The recipe is very simple:

>

>- open your file

>- read one line after another, until you have reached the end

>- everytime, read the line into a String, and add it to your ArrayList

Yes, that seems like a good plan. I'll try that out and see how it works.

Yesterday it was the end of the day, and I wasn't thinking clearly, so I might have sounded a bit crazy, hehe. It's a new day now, and my brain has reset, so it's all good.

Thank you to all who replied with advice, I appreciate it. I'll post an update in awhile after I get something working.

Djaunla at 2007-7-14 20:36:52 > top of Java-index,Java Essentials,Java Programming...
# 15
Hey I found the link at last..Please follow the link below.. http://java.sun.com/developer/technicalArticles/DataTypes/ObjectsConscious/
DeepeshDeomuraria at 2007-7-21 9:59:18 > top of Java-index,Java Essentials,Java Programming...