Class Running Notes 12th Nov
Class Running Notes 12th Nov
define Spliterator<T>?
syntax:
ii
Spliterator<BookDetails> sp = ob.spliterator();
ath
define forEach() method?
aip
=>forEach() method introduced by Java8 version and which is also used to retrieve
faq:
nk
(i)forEachRemaining()
Ve
(ii)forEach()
Spliterator<T> object.
*imp
2.List<E>:
=>List<E> organizes elements based on index values and can hold duplicate
elements.
ii
public abstract int size();
ath
public abstract boolean isEmpty();
aip
public abstract boolean add(E);
ii
ath
public abstract java.util.List<E> subList(int, int);
aip
public abstract java.lang.Object[] toArray();
(a)ArrayList<E>
tes
(b)LinkedList<E>
(c)Vector<E>
a
-----------------------------------------------------------
nk
Ex-program : DemoList1.java
package maccess;
Ve
import java.util.*;
public class DemoList1{
@SuppressWarnings("removal")
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String name=null;
List<Integer> ob = null;
try(s;){
try {
while(true) {
System.out.println("****Choice*****");
System.out.println("1.ArrayList\n2.LinkedList\n3.Vector\n4.exit"
);
System.out.println("Enter the Choice:");
switch(s.nextInt()) {
case 1:
ob = new ArrayList<Integer>();
name="ArrayList";
break;
case 2:
ii
ob = new LinkedList<Integer>();
name="LinkedList";
ath
break;
case 3:
ob = new Vector<Integer>();
name="Vector";
break;
aip
case 4:
System.out.println("Operations stopped
of List");
System.exit(0);
hM
break;
default:
System.out.println("Invalid
Choice...");
}//end of switch
tes
System.out.println("****Operations on
"+name+"****");
xyz:
while(true) {
System.out.println("****Choice****");
a
nk
System.out.println("1.add\n2.remove\n3.add(index,E)\n4.remove(in
dex)\n5.get(index)\n6.set(index,E)\n7.exit");
System.out.println("Enter the
Ve
Choice:");
switch(s.nextInt()) {
case 1:
System.out.println("Enter the
ele:");
ob.add(new Integer(s.nextInt()));
System.out.println(ob.toString());
break;
case 2:
if(ob.isEmpty()) {
System.out.println("List is
empty...");
}else {
System.out.println("Enter the
ele to be removed:");
if(ob.remove(new
Integer(s.nextInt()))) {
System.out.println("Ele
removed Successfully..");
System.out.println(ob.toString());
ii
}else {
System.out.println("Element
ath
not founded...");
}
}
break;
case 3:
aip
if(ob.isEmpty()) {
System.out.println("List is
empty...");
}else {
hM
System.out.println("Enter the
index:");
int index1 = s.nextInt();
if(index1>=0 &&
index1<ob.size()) {
tes
added...");
nk
System.out.println(ob.toString());
}else {
Ve
System.out.println(ob.toString());
}else {
ii
System.out.println("Invalid index value..");
ath
}
}
break;
case 5:
if(ob.isEmpty()) {
aip
System.out.println("List is
empty...");
}else {
System.out.println("Enter the
hM
index:");
int index3 = s.nextInt();
if(index3>=0 &&
index3<ob.size()) {
Integer ele =
tes
(Integer)ob.get(index3);
System.out.println("Ele
at index "+index3+" is "+ele.toString());
System.out.println(ob.toString());
a
}else {
nk
System.out.println("Invalid index...");
}
}
Ve
break;
case 6:
if(ob.isEmpty()) {
System.out.println("List is
empty...");
}else {
System.out.println("Enter the
index:");
int index4 = s.nextInt();
if(index4>=0 &&
index4<ob.size()) {
System.out.println(ob.toString());
}else {
ii
}
}
ath
break;
case 7:
System.out.println("Operations
Stopped on "+name);
break xyz;
aip
default:
System.out.println("Invalid
Choice...");
}//end of switch
hM
}//end of while
}//end of loop
}catch(Exception e) {e.printStackTrace();}
}//end of try
tes
}
}
==============================================================
a
nk
Assignment:
1.addProduct
3.addProduct(index,E)
4.removeProduct(index)===>based on code
5.getProduct(index)===>based on code
6.setProduct(index,E)
7.exit
===============================================================
(a)ArrayList<E>:
class.
ii
ath
Limitation of ArrayList<E>:
aip
backward,and when we perform remove() operation on ArrayList<E> the elements
are moved forward,in this process if we perform more number of add() and remove()
hM
operations then performance of an application is degraded.
Note:
tes
ii
ath
aip
hM
a tes
nk
Ve