-
October 11th, 2004, 07:16 PM
#1
Senior Member
Arrays
Hello Guys.
I want to know how do we initialize the String array in java, when we do not know what will be the size of the array.
like i want to store the following strings in the string array:
------------
102dwd d fdw f fwf
324 sdf sf sf sf s f
dfe4 34 4 r32
-------------------
i am taking these lines from the txt file, and I do not know the size of the array.
I have used: String Arr[];
but the compiler says that i have to initialize the array, but i do not know the size.
Any sort of information will be appreciated.
Regards
Harbir
U get What U pay for.
-
October 11th, 2004, 07:22 PM
#2
I'd use an ArrayList like below:
Code:
import java.util.ArrayList;
public class projectMain {
public static void main(String[] args) {
ArrayList arraylist = new ArrayList();
String x = "hdfaldfadf";
String y = "1556fadsfa";
arraylist.add(x);
arraylist.add(y);
for(int a = 0; a < arraylist.size(); a++) {
System.out.println(arraylist.get(a));
}
}
}
-
October 11th, 2004, 07:55 PM
#3
Senior Member
u r initializing the array, i can't do that, i do not know how many strings will come into the array.
U get What U pay for.
-
October 11th, 2004, 07:58 PM
#4
Write to the string, add it to the ArrayList, then rewrite the string, and add to the ArrayList... repeat or just do arraylist.add("whatever");
-
October 11th, 2004, 08:46 PM
#5
Member
On the whole I think an ArrayList would be your best bet, there are some other alternatives, but all of them involve you essentially rewriting what is done in ArrayList, so just use what you've been given over writing it for yourself.
--BigDick
\"When in Rome, eat Rome!\" -Godzilla
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|