Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Array List

The ArrayList class in Java allows for dynamic resizing, unlike fixed-length arrays, and is implemented using a private array. While it offers more flexibility and powerful methods for manipulation, it is less efficient than arrays and cannot directly hold primitive types without boxing. The document also covers the usage of ArrayLists, including methods for adding, accessing, and modifying elements, as well as the concepts of boxing and unboxing with wrapper classes.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array List

The ArrayList class in Java allows for dynamic resizing, unlike fixed-length arrays, and is implemented using a private array. While it offers more flexibility and powerful methods for manipulation, it is less efficient than arrays and cannot directly hold primitive types without boxing. The document also covers the usage of ArrayLists, including methods for adding, accessing, and modifying elements, as well as the concepts of boxing and unboxing with wrapper classes.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

The ArrayList Class

• ArrayList is a class in the standard Java libraries


– Unlike arrays, which have a fixed length once they have
been created, an ArrayList is an object that can grow
and shrink while your program is running
• In general, an ArrayList serves the same purpose
as an array, except that an ArrayList can change
length while the program is running
The ArrayList Class
• The class ArrayList is implemented using
an array as a private instance variable
– When this hidden array is full, a new larger hidden
array is created and the data is transferred to this
new array
The ArrayList Class
• Why not always use an ArrayList instead of an
array?
1. An ArrayList is less efficient than an array
2. It does not have the convenient square bracket notation
3. The base type of an ArrayList must be a class type
(or other reference type): it cannot be a primitive type
– This last point is less of a problem now that Java provides
automatic boxing and unboxing of primitives
Using the ArrayList Class
• In order to make use of the ArrayList class, it
must first be imported from the package
java.util
• An ArrayList is created and named in the same
way as object of any class, except that you specify
the base type as follows:
ArrayList<BaseType> aList =
new ArrayList<BaseType>();
Using the ArrayList Class
• An initial capacity can be specified when creating an
ArrayList as well
– The following code creates an ArrayList that stores
objects of the base type String with an initial capacity of 20
items
ArrayList<String> list =
new ArrayList<String>(20);
– Specifying an initial capacity does not limit the size to
which an ArrayList can eventually grow
• Note that the base type of an ArrayList is
specified as a type parameter
Using the ArrayList Class
• The add method is used to set an element
for the first time in an ArrayList
list.add("something");
– The method name add is overloaded
– There is also a two argument version that
allows an item to be added at any currently
used index position or at the first unused
position
Using the ArrayList Class
• The size method is used to find out how many
indices already have elements in the
ArrayList
int howMany = list.size();
• The set method is used to replace any existing
element, and the get method is used to access
the value of any existing element
list.set(index, "something else");
String thing = list.get(index);
Tip: Summary of Adding to an
ArrayList
• The add method is usually used to place an
element in an ArrayList position for the
first time (at an ArrayList index)
• The simplest add method has a single
parameter for the element to be added, and
adds an element at the next unused index, in
order
Tip: Summary of Adding to an
ArrayList
• An element can be added at an already
occupied list position by using the two-
parameter version of add
• This causes the new element to be placed at
the index specified, and every other member
of the ArrayList to be moved up by one
position
Tip: Summary of Adding to an
ArrayList
• The two-argument version of add can also be used
to add an element at the first unused position (if that
position is known)
• Any individual element can be changed using the
set method
– However, set can only reset an element at an index that
already contains an element
• In addition, the method size can be used to
determine how many elements are stored in an
ArrayList
Methods in the Class ArrayList
• The tools for manipulating arrays consist only
of the square brackets and the instance
variable length
• ArrayLists, however, come with a
selection of powerful methods that can do
many of the things for which code would have
to be written in order to do them using arrays
Some Methods in the Class
ArrayList (Part 1 of 11)
Some Methods in the Class
ArrayList (Part 2 of 11)
Some Methods in the Class
ArrayList (Part 3 of 11)
Some Methods in the Class
ArrayList (Part 4 of 11)
Some Methods in the Class
ArrayList (Part 6 of 11)
Some Methods in the Class
ArrayList (Part 7 of 11)
The "For Each" Loop
• The ArrayList class is an example of a
collection class
• Starting with version 5.0, Java has added a
new kind of for loop called a for-each or
enhanced for loop
– This kind of loop has been designed to cycle
through all the elements in a collection (like an
ArrayList)
A for-each Loop Used with an
ArrayList (Part 1 of 3)
A for-each Loop Used with an
ArrayList (Part 2 of 3)
A for-each Loop Used with an
ArrayList (Part 3 of 3)
Golf Score Program (Part 1 of 6)
Golf Score Program (Part 2 of 6)
Golf Score Program (Part 3 of 6)
Golf Score Program (Part 4 of 6)
Golf Score Program (Part 5 of 6)
Golf Score Program (Part 6 of 6)
Pitfall: The clone method Makes a Shallow
Copy
• When a deep copy of an ArrayList is needed,
using the clone method is not sufficient
– Invoking clone on an ArrayList object produces a
shallow copy, not a deep copy
• In order to make a deep copy, it must be possible to
make a deep copy of objects of the base type
– Then a deep copy of each element in the ArrayList can
be created and placed into a new ArrayList object
Wrapper Classes
• Wrapper classes provide a class type corresponding
to each of the primitive types
– This makes it possible to have class types that behave
somewhat like primitive types
– The wrapper classes for the primitive types byte, short,
long, float, double, and char are (in order) Byte,
Short, Long, Float, Double, and Character
• Wrapper classes also contain a number of useful
predefined constants and static methods

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-29


• ArrayList <Integer> list = new
ArrayList<Integer> (19);
• List.add(new Integer(6));
• List.add(6);
• Integer x_obj= list.get(0);
• int x = x_obj.intValue();
• int x = list.get(0);
Wrapper Classes
• Boxing: the process of going from a value of a
primitive type to an object of its wrapper class
– To convert a primitive value to an "equivalent" class type
value, create an object of the corresponding wrapper class
using the primitive value as an argument
– The new object will contain an instance variable that
stores a copy of the primitive value
– Unlike most other classes, a wrapper class does not have a
no-argument constructor
Integer integerObject = new Integer(42);

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-31


Wrapper Classes
• Unboxing: the process of going from an object
of a wrapper class to the corresponding value of
a primitive type
– The methods for converting an object from the
wrapper classes Byte, Short, Integer, Long,
Float, Double, and Character to their
corresponding primitive type are (in order)
byteValue, shortValue, intValue,
longValue, floatValue, doubleValue, and
charValue
– None of these methods take an argument
int i = integerObject.intValue();

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-32


Automatic Boxing and Unboxing
• Starting with version 5.0, Java can automatically do boxing
and unboxing
• Instead of creating a wrapper class object using the new
operation (as shown before), it can be done as an automatic
type cast:
Integer integerObject = 42;
• Instead of having to invoke the appropriate method (such as
intValue, doubleValue, charValue, etc.) in order to
convert from an object of a wrapper class to a value of its
associated primitive type, the primitive value can be
recovered automatically
int i = integerObject;

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-33


Automatic Boxing and Unboxing
• ArrayList<Integer> array = new ArrayList<Integer> (2);
• array.add(5);
• array.add(6);
• int x = array.get(1);

You might also like