• Post Reply Bookmark Topic Watch Topic
  • New Topic

Are Arrays are better than ArrayList or in any aspect?  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can any one please tell me how "Arrays are better than ArrayList"?
 
Ranch Hand
Posts: 45
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays Vs ArrayList
 
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually use arrays for internal fixed-size storage. By internal I mean local variables or private fields that are never reachable from the outside.

In this case, I find arrays easier to use. However, I never return arrays from public or protected methods, because they are covariant and break compile time type checking.
 
siva venkata prasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I usually use arrays for internal fixed-size storage. By internal I mean local variables or private fields that are never reachable from the outside.

In this case, I find arrays easier to use. However, I never return arrays from public or protected methods, because they are covariant and break compile time type checking.


Are there any specific advantages of Arrays in searching,sorting,iterating over Arraylist? Any answer where arrays are vastly used?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An ArrayList uses an array in the background, so any advantages arrays do have are going to be almost trivial.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:An ArrayList uses an array in the background, so any advantages arrays do have are going to be almost trivial.


If ArrayList internally users Arrays then what is the reason for Arrays existence ? Can any one quote an example where a piece of work can be done by Arrays where as an ArrayList cant?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javabird user wrote:If ArrayList internally users Arrays then what is the reason for Arrays existence ?

Well, that would be a reason for its existence, wouldn't it? Without arrays, you can't have an ArrayList.
 
javabird user
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew..

I agree. Also any answer for my second question dat "Arrays can do and ArrayList cant do"?
 
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store primitives.
 
Stephan van Hulst
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are also covariant, which allows you to do this:
This however is more of a downside in my book, because it allows you to do this:
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays were available in programming languages long before arrayed lists or similar were developed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!