Is there a Java array / list that is statically typed and variable length

This would be very handy, since typecasting gets boring quickly.

+1


source to share


3 answers


If you are using generics (java 5) you can avoid all castings with

 List<String> myList = new ArrayList<String>();
 myList.add(" a test");
 String temp = myList.get(0);

      



If I am missing something in your question that should cover both needs.

+13


source


I don't understand what is so hard:

List<Foo> fooList = new ArrayList<Foo>();

      

I think you could define a class:



public class FooList extends ArrayList<Foo> {
    ...
}

      

if you want to avoid angle brackets ...

+1


source


If in the variable "length length" you change size over time, then you most likely need a LinkedList, not an ArrayList:

print("List<Foo> fooList = new LinkedList<Foo>();");

      

This way you get the best performance when adding a group of items.

+1


source







All Articles