نسخة من Lab02
نسخة من Lab02
ArrayList Class
Java ArrayList
The ArrayList class is a resizable array, which can be found in the java.util package.
The difference between a built-in array and an ArrayList in Java, is that the size of an
array cannot be modified (if you want to add or remove elements to/from an array, you
have to create a new one). While elements can be added and removed from
an ArrayList whenever you want. The syntax is also slightly different:
Example
Create an ArrayList object called cars that will store strings:
Add Items
The ArrayList class has many useful methods. For example, to add elements to
the ArrayList, use the add() method:
Example
import java.util.ArrayList;
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Access an Item
To access an element in the ArrayList, use the get() method and refer to the index
number:
Example cars.get(0);
Change an Item
To modify an element, use the set() method and refer to the index number:
Example
cars.set(0, "Opel");
Remove an Item
To remove an element, use the remove() method and refer to the index number:
Example
cars.remove(0);
To remove all the elements in the ArrayList, use the clear() method:
Example
cars.clear();
ArrayList Size
To find out how many elements an ArrayList have, use the size method:
Example
cars.size();
Example
public class MyClass {
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars.get(i));
You can also loop through an ArrayList with the for-each loop:
Example
public class MyClass {
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(i);
Other Types
Elements in an ArrayList are actually objects. In the examples above, we created
elements (objects) of type "String". Remember that a String in Java is an object (not a
primitive type). To use other types, such as int, you must specify an equivalent wrapper
class: Integer. For other primitive types, use: Boolean for boolean, Character for
char, Double for double, etc:
Example
Create an ArrayList to store numbers (add elements of type Integer):
import java.util.ArrayList;
myNumbers.add(10);
myNumbers.add(15);
myNumbers.add(20);
System.out.println(i); } }}
Sort an ArrayList
Another useful class in the java.util package is the Collections class, which include
the sort() method for sorting lists alphabetically or numerically:
Example
Sort an ArrayList of Strings:
import java.util.ArrayList;
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(i);
} }}
Example
Sort an ArrayList of Integers:
import java.util.ArrayList;
myNumbers.add(33);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(34);
myNumbers.add(8);
System.out.println(i); } }}
exercises
2- Write a program that lets the user type a random number. Each even
number that is entered should be stored in an ArrayList of
type integer. When the number "-1" is entered then the
program should stop inputting numbers.
3- Write a game program that lets the user type a words. Each
word that is entered must begin by the last character of previous word
to be stored in an ArrayList of
type String. When the word "quit" is entered then the
program should stop inputting string.
4- Write a program that lets the user type a students mark. Each mark
that is entered should be stored in an ArrayList of type integer. Then
check if the mark was grater than or equal 50 then stored in another
ArrayList , and set other marks to be zero. [Exit if user enter -1].
How to ?