0

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

4
  • Must I ask? "What programming language?" Commented Sep 13, 2011 at 19:22
  • The answer depends on what environment you are working in. You'll have to give us a clue! Commented Sep 13, 2011 at 19:22
  • Oh, sorry. I am working in Processing. It's java basically! Commented Sep 13, 2011 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. :) Commented Sep 14, 2011 at 21:16

5 Answers 5

4
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.

Sign up to request clarification or add additional context in comments.

8 Comments

Keep 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!
@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.
This is a Processing.org question, right? Does processing.org support Java-5 yet?
@TimothyGroote My mistake, I saw the Java tag and stopped looking there..!
You 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 processing.org/learning/eclipse, you can do a similar setup for Netbeans if you like.
|
0

You can use ArrayList: http://processing.org/reference/ArrayList.html

Comments

0

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.

Comments

0

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.

Comments

-2

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.

5 Comments

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
get(int index) returns the String at the specified position in the list (use add(String) to put them there in the first place).
ArrayList 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).
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.