Set an array with undeterminate size
I should know this by now, but go figure. If I want to set up an array of Strings, yet I do not know how many Strings I am going to stick in it, how would I do so? For example, if I want to create an array myArray, and I do not know whether I am going to put 15 elements in it or 500, how would I set the length to be indeterminate, or something like that, so I do not get an ArrayIndexOutOfBoundsException?
Thanks,
Dan
[438 byte] By [
Djaunla] at [2007-10-3 2:47:43]

Does it HAVE to be an array or can it be a vector or a collection (I believe) or something along those lines ? *Note* I think it's a collection, not sure as the last time I needed a large group of things I had a vector at my disposal.
You could use a List, such as ArrayList.
How are you filling the array? Will you know the number of Strings at some point before filling it (at runtime, but not at compile time)? If you won't even know at runtime, and want to be sure not to get that exception, try an ArrayList. It will grow for you as necessary.
MLRona at 2007-7-14 20:36:30 >

Ah ok. I am actually using an ArrayList right now, I just figured there would be a way to use normal arrays.
And the number of strings I receive depends on how many strings the user inputs. He or she could input one, or he or she could input fifteen. I have a way of getting the number to my Java file obviously, so I will know the number of strings, sort of.
Thanks for the help,
Dan
If you know at runtime how many Strings you'll have, you can do this:
int numStrings = ...; // get from user (or whatever)
String [] strings = new String[numStrings];
If you don't even know at runtime until you have all String values, ArrayList is probably just as easy as an array.
MLRona at 2007-7-14 20:36:30 >

Ah, that's a good idea. Thank you.
Actually, while we're talking about arrays, is there some sort of way to specify where an iterator is in an array during a for loop?
In other words, if I have an array of strings, and want my code to do something if the element in the array is greater than 2, how would I go about doing that? In case that didn't make sense, let me provide an example.
int[] anArray = {1, 2, 3};
for (int i : anArray) {
if (i > 0) {
System.out.println("This number is greater than 1");
}
else {
System.out.println("This number is the number 1");
}
}
I'm basically trying to say the same thing, except using an array of strings. In the above example, if i is greater than 0, i.e. it is on the first or second value of the array (2 and 3, respectively) it will print out that the number is greater than 1. Otherwise, it will say the number is 1.
So my question now is: how could I do something similar using an array of strings? If the iterator is on the second string element in the array or greater, how could I signify that?
Please let me know if that doesn't make sense. And thank you for your help.
Dan
Actually, I figured it out after I played around some. The enhanced for loop does not work for this; instead I used the standard one, and said this:
String[] fileVars = {fileVar1, fileVar2, fileVar3};
for (int i=1; i < fileVars.length; i++) {
if (i > 1) {
// Do things here
}
}
Sorry if my previous post inconvienced anyone; I should have looked around more before I posted it.
> Sorry if my previous post inconvienced anyone; I should have looked around more before I posted it.
No problem. Good that you kept going and tried it out yourself, instead of just waiting for an answer. I wouldn't have known offhand--I've never used the enhanced 'for' loop (because the Java project I was on used Java 1.4 [1.3 until last December]).
MLRona at 2007-7-14 20:36:30 >

He tricked you guys.
CROSS POST!
http://forum.java.sun.com/thread.jspa?threadID=760485&tstart=0
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.
Sorry to bump this, but I just want to apologize if anyone thinks I cross-posted. I made a thread in "Java Programming" about similar concepts, but I believe it is a different question. I explained my reasoning in that thread, if you want to check it out.
In short: I did not mean to cross-post the same question; as TuringPest pointed out, that would be rude. If I accidentally did (i.e. if I didn't recognize that the problems were the same) then I apologize. It was not my intent to cross-post the same question.