0% found this document useful (0 votes)
11 views13 pages

نسخة من Lab02

The document discusses the ArrayList class in Java, including how to create ArrayLists, add and remove elements, access elements, loop through elements, and sort ArrayLists. It also provides examples of using ArrayLists to store different data types like Strings and Integers. The document ends with exercises for practicing ArrayLists.

Uploaded by

termedelox1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

نسخة من Lab02

The document discusses the ArrayList class in Java, including how to create ArrayLists, add and remove elements, access elements, loop through elements, and sort ArrayLists. It also provides examples of using ArrayLists to store different data types like Strings and Integers. The document ends with exercises for practicing ArrayLists.

Uploaded by

termedelox1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lab #02

Al_Balqa’ Applied University


IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

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:

import java.util.ArrayList; // import the ArrayList class

ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList object

Add Items
The ArrayList class has many useful methods. For example, to add elements to
the ArrayList, use the add() method:

Rasha Moh'd Altarawneh Page 1 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

Example
import java.util.ArrayList;

public class MyClass {

public static void main(String[] args) {

ArrayList<String> cars = new ArrayList<String>();

cars.add("Volvo");

cars.add("BMW");

cars.add("Ford");

cars.add("Mazda");

System.out.println(cars); }} \\ [Volvo, BMW, Ford, 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);

Rasha Moh'd Altarawneh Page 2 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

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();

Rasha Moh'd Altarawneh Page 3 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

ArrayList Size
To find out how many elements an ArrayList have, use the size method:

Example
cars.size();

Loop Through an ArrayList


Loop through the elements of an ArrayList with a for loop, and use the size() method to
specify how many times the loop should run:

Example
public class MyClass {

public static void main(String[] args) {

ArrayList<String> cars = new ArrayList<String>();

cars.add("Volvo");

Rasha Moh'd Altarawneh Page 4 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

cars.add("BMW");

cars.add("Ford");

cars.add("Mazda");

for (int i = 0; i < cars.size(); i++) {

System.out.println(cars.get(i));

You can also loop through an ArrayList with the for-each loop:

Example
public class MyClass {

public static void main(String[] args) {

ArrayList<String> cars = new ArrayList<String>();

cars.add("Volvo");

cars.add("BMW");

Rasha Moh'd Altarawneh Page 5 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

cars.add("Ford");

cars.add("Mazda");

for (String i : cars) {

System.out.println(i);

Rasha Moh'd Altarawneh Page 6 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

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;

public class MyClass {

public static void main(String[] args) {

ArrayList<Integer> myNumbers = new ArrayList<Integer>();

myNumbers.add(10);

myNumbers.add(15);

myNumbers.add(20);

for (int i : myNumbers) {

System.out.println(i); } }}

Rasha Moh'd Altarawneh Page 7 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

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;

import java.util.Collections; // Import the Collections class

public class MyClass {

public static void main(String[] args) {

ArrayList<String> cars = new ArrayList<String>();

cars.add("Volvo");

cars.add("BMW");

cars.add("Ford");

cars.add("Mazda");

Collections.sort(cars); // Sort cars

Rasha Moh'd Altarawneh Page 8 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

for (String i : cars) {

System.out.println(i);

} }}

Example
Sort an ArrayList of Integers:

import java.util.ArrayList;

import java.util.Collections; // Import the Collections class

public class MyClass {

public static void main(String[] args) {

ArrayList<Integer> myNumbers = new ArrayList<Integer>();

myNumbers.add(33);

myNumbers.add(15);

myNumbers.add(20);

myNumbers.add(34);

myNumbers.add(8);

Rasha Moh'd Altarawneh Page 9 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

Collections.sort(myNumbers); // Sort myNumbers

for (int i : myNumbers) {

System.out.println(i); } }}

Rasha Moh'd Altarawneh Page 10 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

exercises

1- Write a program that lets the user type in names. Each


name that is entered should be stored in an ArrayList of
type String. When the word "quit" is entered then the
program should stop inputting names.

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].

Rasha Moh'd Altarawneh Page 11 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

5- Write a method switchPairs that switches the order of values in an


ArrayList of Strings in a pairwise fashion. Your method should switch the order of the first
two values, then switch the order of the next two, switch the order of the next two, and so
on. For example, if the list initially stores these values: {"four", "score", "and", "seven",
"years", "ago"} your method should switch the first pair, "four", "score", the second pair,
"and", "seven", and the third pair, "years", "ago", to yield this list: {"score", "four", "seven",
"and", "ago", "years"} If there are an odd number of values in the list, the final element is
not moved. For example, if the original list had been: {"to", "be", "or", "not", "to", "be",
"hamlet"} It would again switch pairs of values, but the final value, "hamlet" would not be
moved, yielding this list: {"be", "to", "not", "or", "be", "to", "hamlet"}

6- Write a method markLength4 that takes an ArrayList of Strings as a parameter


and that places a string of four asterisks "****" in front of every string of length 4. For
example, suppose that a variable called list contains the following values: {"this", "is",
"lots", "of", "fun", "for", "every", "Java", "programmer"} And you make the following call:
markLength4(list); then list should store the following values after the call: {"****", "this",
"is", "****", "lots", "of", "fun", "for", "every", "****", "Java", "programmer"} Notice that you
leave the original strings in the list, "this", "lots", "Java", but include the fourasterisk string
in front of each to mark it.

Rasha Moh'd Altarawneh Page 12 of 13


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

How to ?

Rasha Moh'd Altarawneh Page 13 of 13

You might also like