Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

oop isddjfvfcncmcxmcdmcmc v v djddjddjdjdjd, Lecture notes of Programming Paradigms

array list is a list of an array we use in java programming cnfjdkcjk jddjjskj

Typology: Lecture notes

2019/2020

Uploaded on 02/19/2020

hassanmoh_10
hassanmoh_10 🇸🇦

2 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Copyright 2008 by Pearson Education
Building Java Programs
Chapter 10
Lecture 10-1: ArrayList
reading: 10.1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Related documents


Partial preview of the text

Download oop isddjfvfcncmcxmcdmcmc v v djddjddjdjdjd and more Lecture notes Programming Paradigms in PDF only on Docsity!

Building Java Programs

Chapter 10

Lecture 10-1: ArrayList

reading: 10.

2 Exercise

! Write a program that reads a file and displays

the words of that file as a list.

! First display all words. ! Then display them with all plurals (ending in "s") capitalized. ! Then display them in reverse order. ! Then display them with all plural words removed.

! Should we solve this problem using an array?

! Why or why not?

4 Lists

! list : a collection storing an ordered sequence of elements

! each element is accessible by a 0-based index ! a list has a size (number of elements that have been added) ! elements can be added to the front, back, or elsewhere ! in Java, a list can be represented as an ArrayList object

5 Idea of a list

! Rather than creating an array of boxes, create an object

that represents a "list" of items. (initially an empty list.)

[]

! You can add items to the list.

! The default behavior is to add to the end of the list. [hello, ABC, goodbye, okay]

! The list object keeps track of the element values that have

been added to it, their order, indexes, and its total size.

! Think of an "array list" as an automatically resizing array object. ! Internally, the list is implemented using an array and a size field.

7 Type Parameters (Generics)

ArrayList< Type > name = new ArrayList< Type >();

! When constructing an ArrayList, you must specify the

type of elements it will contain between < and >.

! This is called a type parameter or a generic class. ! Allows the same ArrayList class to store lists of different types. ArrayList names = new ArrayList (); names.add("Marty Stepp"); names.add("Stuart Reges");

8 Learning about classes

! The Java API Specification is a huge web page containing

documentation about every Java class and its methods.

! The link to the API Specs is on the course web site.

10 ArrayList vs. array 2

! doing something to each value that starts with "B"

for (int i = 0; i < names.length; i++) { if (names[i].startsWith("B")) { ... } } for (int i = 0; i < list.size(); i++) { if (list.get(i).startsWith("B")) { ... } }

! seeing whether the value "Benson" is found

for (int i = 0; i < names.length; i++) { if (names[i].equals("Benson")) { ... } } if (list.contains("Benson")) { ... }

11 Exercise, revisited

! Write a program that reads a file and displays

the words of that file as a list.

! First display all words. ! Then display them in reverse order. ! Then display them with all plurals (ending in "s") capitalized. ! Then display them with all plural words removed.

13 ArrayList as parameter

public static void name (ArrayList< Type > name ) {

! Example:

// Removes all plural words from the given list. public static void removePlural( ArrayList list ) { for (int i = 0; i < list.size(); i++) { String str = list.get(i); if (str.endsWith("s")) { list.remove(i); i--; } } }

! You can also return a list:

public static ArrayList< Type > methodName ( params )

14 ArrayList of primitives?

! The type you specify when creating an ArrayList must be

an object type; it cannot be a primitive type.

// illegal -- int cannot be a type parameter ArrayList list = new ArrayList ();

! But we can still use ArrayList with primitive types by

using special classes called wrapper classes in their place.

// creates a list of ints ArrayList list = new ArrayList ();

16 Exercise

! Write a program that reads a file full of numbers and

displays all the numbers as a list, then:

! Prints the average of the numbers. ! Prints the highest and lowest number. ! Filters out all of the even numbers (ones divisible by 2).

17 Exercise solution (partial) ArrayList numbers = new ArrayList(); Scanner input = new Scanner(new File("numbers.txt")); while (input.hasNextInt()) { int n = input.nextInt(); numbers.add(n); } System.out.println(numbers); filterEvens(numbers); System.out.println(numbers); ... // Removes all elements with even values from the given list. public static void filterEvens (ArrayList list) { for (int i = list.size() - 1; i >= 0; i--) { int n = list.get(i); if (n % 2 == 0) { list.remove(i); } } }

19 Out-of-bounds

! Legal indexes are between 0 and the list's size() - 1.

! Reading or writing any index outside this range will cause an IndexOutOfBoundsException. ArrayList names = new ArrayList(); names.add("Marty"); names.add("Kevin"); names.add("Vicki"); names.add("Larry"); System.out.println(names.get(0)); // okay System.out.println(names.get(3)); // okay System.out.println(names.get(-1)); // exception names.add(9, "Aimee"); // exception

index 0 1 2 3

value Marty Kevin Vicki Larry

20 ArrayList "mystery" ArrayList list = new ArrayList(); for (int i = 1; i <= 10; i++) { list.add(10 * i); // [10, 20, 30, 40, ..., 100] }

! What is the output of the following code?

for (int i = 0; i < list.size(); i++) { list.remove(i); } System.out.println(list);

! Answer:

[20, 40, 60, 80, 100]