.ArchJava Lesson 10-5 (List)
.ArchJava Lesson 10-5 (List)
Framework in Java
©https://blogs.ashrithgn.com/
Objectives
• Collection interfaces:
• Represent different types of collections, such as sets, lists, and maps. These
interfaces form the basis of the framework.
java.util
package
contains all the
classes and
interfaces for
the Collection
framework
https://www.javatpoint.com/collections-in-java
Faculty of Computer Science ©https://blogs.ashrithgn.com/
What is List?
• List interface is found in the java.util package and inherits the Collection
interface. java.util.Collection
• The implementation classes of List interface :
• ArrayList
• LinkedList
• Stack
• Vector
Faculty of Computer Science ©https://blogs.ashrithgn.com/
List Declaration and Creation
• Declaration:
interface-name<element-type> identifier;
• Creation:
identifier = new class-name<element-type> (parameters);
Method Description
add() adds an element to a list
addAll() adds all elements of one list to another
get() helps to randomly access elements from lists
iterator() returns iterator object that can be used to sequentially access elements of lists
• Auto boxing : With a list, we can store only objects. If we need to store primitive
data values in a list, then we must use wrapper classes such as
Integer, Float, and Double.
There are no restrictions on
the type of objects we can
List<Integer> intList = new ArrayList<Integer>(); add to the list.
intList.add(15);
intList.add(30); the compiler translates it to
…
int sum=0; intList.add(new Integer(30));
for ( int value : intList){
sum+=value; Automatic boxing
}
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Automatic Unboxing
• Auto unboxing :
Automatic unboxing
package bosjavaprogram;
import java.util.*;
public class ListDemo {
public static void main(String[] args) {
List<Integer> list1= new ArrayList<Integer>();
list1.add(0, 1); Add element to list at
list1.add(1, 2); specific index position
System.out.println(list1); [1, 2]
List<Integer> list2= new ArrayList<Integer>();
list2.add(1);
list2.add(2);
list2.add(3);
System.out.println(list2); [1, 2, 3]
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Cont’d
list1.addAll(1, list2);
Output:
System.out.println("After inserting list2 to
list1 at position 1: "+list1);
int index = list1.indexOf(2);
System.out.println("Position of 3 is " + index);
list1.remove(1);
System.out.println(list1);
System.out.println(list1.get(3));
list1.set(0, 5); Change element
System.out.println(list1); specific index position
}
} Faculty of Computer Science ©https://blogs.ashrithgn.com/
List with Object Data Type
package bosjavaprogram;
import java.util.*;
class Book {
int id;
String name, author, publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
} Faculty of Computer Science ©https://blogs.ashrithgn.com/
Cont’d
ListIterator<String> itr=al.listIterator( );
• What is List ?
• ListDemo.java
• Book.java
• ListIteratorExample1.java
https://docs.oracle.com/javase/8/docs/
technotes/guides/collections/overview.html
Faculty of Computer Science ©https://blogs.ashrithgn.com/
What will be next?