5 array list, data structure course

11
using Java 2015 Data Structure Prepared by: Mahmoud Rafeek Al-farra in Java 5. Array List

Upload: mahmoud-alfarra

Post on 15-Jan-2017

391 views

Category:

Education


2 download

TRANSCRIPT

Page 1: 5 Array List, data structure course

using Java

2015

Data Structure Prepared by: Mahmoud Rafeek Al-farra

in Java

5. Array List

Page 2: 5 Array List, data structure course

mfarra.cst.ps www.fb.com/MahmoudRFarra

Contents

Example

The ArrayList Class

Introduction

Comparison between Arrays and ArrayList

Page 3: 5 Array List, data structure course

Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra

You can create an array to store objects. But, once the array is created, its size is fixed.

Nevertheless, you can still use arrays to implement dynamic data structures.

The trick is to create a larger new array to replace the current array, if the current array cannot hold new elements in the list.

Page 4: 5 Array List, data structure course

Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra

You can see an animated example from the following link:http://www.cs.armstrong.edu/liang/animation/web/ArrayList.html

Page 5: 5 Array List, data structure course

The ArrayList Classmfarra.cst.ps www.fb.com/MahmoudRFarra

Java provides the ArrayList class, which can be used to store an unlimited number of objects.

ArrayList is known as a generic class with a generic type E.

You can specify a concrete type to replace E when creating an ArrayList.

For example:ArrayList <String> cities = new ArrayList<> ();

Page 6: 5 Array List, data structure course

The ArrayList Classmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 7: 5 Array List, data structure course

Example: The ArrayList Classmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 8: 5 Array List, data structure course

Example: The ArrayList Classmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 9: 5 Array List, data structure course

Comparison between Arrays and ArrayList

mfarra.cst.ps www.fb.com/MahmoudRFarra

Page 10: 5 Array List, data structure course

Array Listmfarra.cst.ps www.fb.com/MahmoudRFarra

The ArrayList is in the java.util package, it is imported in line 1.

Case Study: Develop a program that prompts the user to enter a sequence of numbers and displays the distinct numbers in the sequence. Assume that the input ends with 0 and 0 is not counted as a number in the sequence.

[ind, N.W]

Page 11: 5 Array List, data structure course

using Java

2015

FB: M a h m o u d R F a r r aYouTube: M a h m o u d R F a r SlidesShare: mralfarra

Thank you