• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array vs. ArrayList  RSS feed

 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nil wangad wrote:
how to create dynamic string array if we dont know number of strings in the beginning?


Java has arrays that can grow, like ArrayList.

Just declare the ArrayList to be of String and you can add as many String objects you want.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindy Lindqvist wrote:
nil wangad wrote:
how to create dynamic string array if we dont know number of strings in the beginning?

Java has arrays that can grow, like ArrayList.


Technically, you are wrong. ArrayList is not an array. It is a list. The fact that is based on array does not matter.
Arrays in Java can't grow.
ArrayList uses an array internally. If you want do add an element and the array is too small, ArrayList will copy all its contents to bigger array.
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Technically, you are wrong. ArrayList is not an array. It is a list.


No I'm technically right and you are nitpickingly wrong.

The ArrayList is a dynamic array implementation part of the Java standard API. Dynamic means it can grow at runtime.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindy Lindqvist wrote:
Pawel Pawlowicz wrote:
Technically, you are wrong. ArrayList is not an array. It is a list.


No I'm technically right and you are nitpickingly wrong.

The ArrayList is a dynamic array implementation part of the Java standard API. Dynamic means it can grow.

Yes, ArrayList can grow.
No, ArrayList is not an array. It is a List implementation that uses an array internally.
In Java arrays have fixed size. They can't grow. Even those used by ArrayList!
If an ArrayList finds out that its array is too small it creates larger one (still fixed size!) and forgets about the smaller one.
And yes, I am nitpicking
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindy Lindqvist wrote:No I'm technically right and you are nitpickingly wrong.

Sorry, but you are not correct. ArrayList is not an array; it is a List implementation. With a List (ArrayList included), you cannot use array indexing, and there is no length property.

Saying that ArrayList is an array is not just technically incorrect, it's even conceptually incorrect.
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
No, ArrayList is not an array.


Yes it is. It's a dynamic array.

You're confusing design with implementation.

 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Lindy Lindqvist wrote:No I'm technically right and you are nitpickingly wrong.

Sorry, but you are not correct. ArrayList is not an array; it is a List implementation. With a List (ArrayList included), you cannot use array indexing, and there is no length property.

Saying that ArrayList is an array is not just technically incorrect, it's even conceptually incorrect.


Don't confuse the List interface with the ArrayList concrete class.

The ArrayList implements the List interface for sure but that doesn't change the fact that ArrayList is a dynamic array implementation.

So you're wrong. The ArrayList is a dynamic array, nothing else. Period.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If x is a reference to an ArrayList, and it's an array (according to you), then shouldn't we be able to write "x[2]" and "x.length"? After all you can do that for arrays.

But you can't. So it isn't correct to say an ArrayList is an array. It may well be correct that an ArrayList has an array, but that isn't the same thing. The distinction is well-known in the field of object-oriented design.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to compile this:results in

Tmp.java:6: error: inconvertible types
System.out.println(a instanceof Object[]);
^
required: Object[]
found: ArrayList


Is Java compiler also wrong?

Note: Object[] is a superclass for all arrays in Java


Give it a chance:
Tmp.java:7: error: array required, but ArrayList found
System.out.println(a[0]);


Nope. ArrayList is not an array.
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:If x is a reference to an ArrayList, and it's an array (according to you), then shouldn't we be able to write "x[2]" and "x.length"? After all you can do that for arrays.

But you can't. So it isn't correct to say an ArrayList is an array. It may well be correct that an ArrayList has an array, but that isn't the same thing. The distinction is well-known in the field of object-oriented design.


Well, I said ArrayList is a dynamic array implementation. And it is. I didn't claim ArrayList would be exacly like a native Java array. The Java language isn't flexible enougth to make such an implementation possible.

But okay I'll rephrase. The ArrayList is a dynamic array implementation but it's only as close to a native Java array as the Java language permits (and in that sense ArrayList doesn't differ from any other class in Java that's not part of the core language or has special language support).
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Nope. ArrayList is not an array.


Yes it is.

ArrayList is a dynamic array implementation and it's as close to a native array as the Java language permits a class to be. And it's part of the standard API.

This means when a static Java array doesn't suit you, first have a look at the ArrayList.

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindy Lindqvist wrote:
Pawel Pawlowicz wrote:
Nope. ArrayList is not an array.


Yes it is.

ArrayList is a dynamic array implementation and it's as close to a native array as the Java language permits a class to be. And it's part of the standard API.

This means when a static Java array doesn't suit you, first have a look at the ArrayList.


Technically, static in Java means... Ehhh. Never mind...
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Technically, static in Java means... Ehhh. Never mind...


You don't know the difference between a static and a dynamic array?

Then why did you enter this discussion at all. To nitpick?

In that case why don't you complain where it's due, namely at the Java language.

The ArrayList is a dynamic array implementation but it still lacks many features of a native Java array. Why? Simply because Java doesn't allow it to get closer. Java doesn't support things like operator overloading for example. That would be something of substance to complain about.

Furthermore don't even try besserwisser insinuations with me. Good programmers easily can jump between the general notion of a static array and specific uses of the static keyword in Java. Or are you challenged for real?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've extracted this conversation from here.

Please continue the array vs. ArrayList discussion here if you must, but at this point I'd suggest to consult the JLS to anyone who wants to get an authoritative answer about what the term "array" refers to in Java.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!