I want to create an array of strings, but I do not know the length of it in the beginning. It's like the array length depends on many factors and it's only decided when I fill strings/words into it. however, processing does not allow me to do that, it asks me to specify the length in the beginning. How can I get rid of this?..Thanks for all help. Any suggestion will be appreciated. Amrita
-
Must I ask? "What programming language?" – Kirk Woll Sep 13 '11 at 19:22
-
The answer depends on what environment you are working in. You'll have to give us a clue! – Bill Michell Sep 13 '11 at 19:22
-
Oh, sorry. I am working in Processing. It's java basically! – Amrita Sep 13 '11 at 19:28
-
@Amrita, ha, I assumed you were asking a question about the concept of "processing", which seemed vague. Now I know it's also a programming language. :) – Kirk Woll Sep 14 '11 at 21:16
5 Answers
List<String> strs = new ArrayList<String>();
strs.add("String 1");
strs.add("String 2");
strs.add("String 3");
System.out.println(strs.size()); //3
System.out.println(strs.get(1)); //String 2
Something like that is all you need! You don't need to worry about resizing, copying stuff in memory or whatever - the list will just expand as it needs to. All of the performance details are taken care of and unless you're really interested in how it works, you don't need to read about those details to use it.

- 70,193
- 21
- 157
- 216
-
1Keep in mind though, that any item you "get" from your Arraylist will need to be casted, since the compiler has no idea what object you put in the Arraylist! – Timothy Groote Oct 14 '11 at 09:42
-
@TimothyGroote Not correct - that's what generics are for! Unless you're using pre Java-5 of course, but then the above code wouldn't compile. – Michael Berry Oct 14 '11 at 10:57
-
This is a Processing.org question, right? Does processing.org support Java-5 yet? – Timothy Groote Oct 14 '11 at 11:59
-
@TimothyGroote My mistake, I saw the Java tag and stopped looking there..! – Michael Berry Oct 14 '11 at 12:17
-
don't sweat it :) i do *wish* processing would support it, i'm used to C#, and all that casting drives me nuts – Timothy Groote Oct 17 '11 at 11:08
-
1You can't do it in the processing IDE, but you CAN use Processing as a java library. That way you'll be writing standard Java code and can use whatever Java features (and Java IDE) you like. See http://processing.org/learning/eclipse/, you can do a similar setup for Netbeans if you like. – PapaFreud Nov 14 '11 at 09:02
-
@PapaFreud Actually you can do this in the IDE, and since the ArrayList is typed in berry's example, using get() will return a String, not an Object. – George Profenza Nov 25 '11 at 20:37
-
Wait, what? Processing supports Java Generics now? I had to try it, and yes - my Processing 1.5.1 (on OS X) does seem to work with Java Generics. Cool! I stand corrected. And +1 for @berry120's reply then, that's how to do it. – PapaFreud Nov 28 '11 at 09:01
You can use ArrayList: http://processing.org/reference/ArrayList.html

- 106,652
- 57
- 273
- 297
I would start by using ArrayList and resizing it when necessary. Java pre-allocates memory for ArrayList so that not every resize means that the contents are copied in memory. Access to ArrayList is faster than to LinkedList (it's O(1) instead of O(n)). Only if you find that the resizing of the ArrayList takes too much time, would I think of switching to LinkedList.

- 6,181
- 1
- 34
- 57
Use the typed ArrayList as @berry120 suggests (otherwise, you'll need to cast from Object to String all the time).
Also, if it helps, Processing has some functions for handling Arrays (like append() and expand()). Look under Array Functions in the Processing reference.
Behind the scenes the above mentioned Array Functions use System.arraycopy(), if that's of any use.

- 50,687
- 19
- 144
- 218
You need to use a LinkedList structure: this gives you an easily expanded container array and takes an initial capacity in the constructor, rather than a set limit. This will also be more efficient than an ArrayList, which will copy it's contents every time you exceed the current capacity, rather than simply add to it.

- 452
- 2
- 3
-
thanks a lot! How do I access elements out of the LinkedList? I do not know the right syntax. Thanks again for your help. Amrita – Amrita Sep 13 '11 at 20:23
-
get(int index) returns the String at the specified position in the list (use add(String) to put them there in the first place). – Adrian Taylor Sep 13 '11 at 20:36
-
2ArrayList does not copy its contents *every* time you resize it, because it pre-allocates the memory when you resize it, multiplying the current storage size by a fixed factor (1.2 AFAIR). – quant_dev Sep 17 '11 at 19:24
-
Why on earth would you use a linked list as oppose to an arraylist? I can count the times I've used the former on one hand, ArrayList is by far the most generally useful and most generally efficient type. Your comment about efficiency is also misleading as per quant_dev's comment. Most common operations (such as iterating over the list) are incredibly inefficient in terms of linked lists. They only really come into play when you're adding and removing lots of elements at random positions, and even then the increased performance with these operations needs to be weighed up against the negatives – Michael Berry Sep 17 '11 at 19:43
-
It depends what you are doing really. ArrayLists are also my standard container of choice, but there are times when they can be inefficient. Sometimes I have to build very large lists of indeterminate size, and rather than specify an ArrayList with a huge capacity (or have it copy itself every time it exceeds its memory allocation) I use a LinkedList to let it grow as needed: just seems more elegant. Guess I read more into the question than necessary: to which I was really implying just use a List of some type anyway. – Adrian Taylor Sep 19 '11 at 22:55