0% found this document useful (0 votes)
7 views7 pages

LIST

LIST in Java

Uploaded by

Saeed Akhter
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)
7 views7 pages

LIST

LIST in Java

Uploaded by

Saeed Akhter
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/ 7

LIST

List in Java provides the facility to maintain


the ordered collection. It contains the index-
based methods to insert, update, delete and
search the elements. It can have the duplicate
elements also. We can also store the null
elements in the list.
The implementation classes of List interface
are ArrayList, LinkedList, Stack and Vector. The
ArrayList and LinkedList are widely used in Java
programming.
import java.util.ArrayList;
import java.util.Collections;

class ArrayLists {
public static void main(String args[]) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<String> list2 = new ArrayList<String>();
ArrayList<Boolean> list3 = new ArrayList<Boolean>();

//add elements
list.add(1);
list.add(3);
list.add(4);
list.add(5);
System.out.println(list);

//to get an element


int element = list.get(0); // 0 is the index
System.out.println(element);

//add element in between


list.add(1,2); // 1 is the index and 2 is the element to be added
System.out.println(list);

//set element
list.set(0,0);
System.out.println(list);

//delete elements
list.remove(0); // 0 is the index
System.out.println(list);

//size of list
int size = list.size();
System.out.println(size);

//Loops on lists
for(int i=0; i<list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();

//Sorting the list


list.add(0);
Collections.sort(list);
System.out.println(list);
}
}
Java boolean list is used to store boolean data
type values only . The default value of the
boolean elements in a Java boolean array is
false . The default value for a Boolean wrapper
class object is null . The default value for
a boolean primitive type is false.
boolean isEmpty() It returns true if the list is empty,
otherwise false.
class List{
private int[] array={0,1,2,3,4,5,6,7,8,9};
private int sz=10;
public boolean IsFull()
{
if (sz==10)
return true;
else
return false;
}
public boolean IsEmpty()
{
if(sz==0)
return true;
else
return false;
}
public void Insert(int data)
{
if(IsFull())
System.out.println("list is Full");
else
array[sz]=data;
sz++;
}
public void Remove()
{
if(IsEmpty())
System.out.println("List is Empty");
else
sz--;
}
public void Display()
{
for (int i=0;i<sz;i++)
System.out.println(array[i]+"");
}
public static void main(String[] args){
List obj=new List();
obj.Display();
}
}

You might also like