Monday, February 21, 2011

Chapter 40: ArrayList Basics

We saw how to create and use Arrays in Chapter . Though Arrays are very efficient, the programmer has to ensure that he keeps the index in mind and has code that will handle the inserts/updates and deletes into the array. Though the previous sentence might sound simple, implementing it in code is not as simple. To help us not go through the ordeal of doing that, the java language creators provide us with the ArrayList. In this chapter we are going to take a look at this all powerful class.

So, lets get started!!!

ArrayLists

The java.util.ArrayList class is one of the most commonly used of all the classes in the Collections Framework. Some of the advantages ArrayList has over arrays are

• It can grow dynamically.
• It provides more powerful insertion and search mechanisms than arrays.

Let’s take a look at using an ArrayList that contains Strings. A key design goal of the Collections Framework was to provide rich functionality at the level of the main interfaces: List, Set, and Map. In practice, you’ll typically want to instantiate an ArrayList polymorphically like this:

List myFirstArrayList = new ArrayList();

As of Java 5 you’ll want to say
List myFirstArrayList = new ArrayList();

This kind of declaration follows the object oriented programming principle of “coding to an interface”, and it makes use of generics. We’ll say lots more about generics in future, but for now just know that, as of Java 5, the syntax is the way that you declare a collection’s type. (Prior to Java 5 there was no way to specify the type of a collection, and when we cover generics, we’ll talk about the implications of mixing Java 5 (typed) and pre-Java 5 (untyped) collections.)

In many ways, ArrayList is similar to a String[] in that it declares a container that can hold only Strings, but it’s more powerful than a String[]. Let’s look at some of the capabilities that an ArrayList has

List test = new ArrayList();
String s = "hi";
test.add("string");
test.add(s);
test.add(s+s);
System.out.println(test.size());
System.out.println(test.contains(42));
System.out.println(test.contains("hihi"));
test.remove("hi");
System.out.println(test.size());

which produces
3
false
true
2

There’s lots going on in this small program. Notice that when we declared the ArrayList we didn’t give it a size. Then we were able to ask the ArrayList for its size, we were able to ask it whether it contained specific objects, we removed an object right out from the middle of it, and then we rechecked its size.

Autoboxing with Collections

In general, collections can hold Objects but not primitives. Prior to Java 5, a very common use for the wrapper classes was to provide a way to get a primitive into a collection. Prior to Java 5, you had to wrap a primitive by hand using one of the constructors of that type, before you could put it into a collection. With Java 5, primitives still have to be wrapped, but autoboxing takes care of it for you.
Checkout the Wrapping & Boxing chapter by clicking here (If you want to refresh that topic)

List myFirstIntArray = new ArrayList(); // pre Java 5 declaration
myFirstIntArray.add(new Integer(42)); // had to wrap an int

As of Java 5 we can say
myFirstIntArray.add(42); // autoboxing handles it!

In this last example, we are still adding an Integer object to myFirstIntArray (not an int primitive); it’s just that autoboxing handles the wrapping for us.

Previous Chapter: Chapter 39 - Getting Started with Collections

Next Chapter: Chapter 41 - Sorting Collections & Arrays

5 comments:

  1. Hi,
    Autoboxing is a great feature provided by JAVA5 but using it blindly may result in very subtle problem which will take hours and hours to
    debug and find. to read more see the link
    What is the problem while using '==' in autoboxing world in Java 5 ?

    Thanks
    Javin

    ReplyDelete
  2. This is a nice article..
    Its very easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial

    Thanks a lot..!

    ReplyDelete
  3. This article is really very informative. Please write more on this because I understand this very easily. It helps me in my studies.
    .net obfuscators

    ReplyDelete
  4. Thanks for commenting on my blog How to Convert arraylist to array in Java Anand. you too has great blog and quite detailed information.

    ReplyDelete
  5. nice explanation...

    here you can find ArrayList to HashSet Convertion

    ArrayList to HashSet

    ReplyDelete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers