Structure of the Problem Requirements
In this problem we will learn about the List in Java.The
java.util.List
interface is a subtype of the Java.util.Collection interface. It represents an ordered list of objects, meaning you can access the elements of a List
in a specific order, and by an index too. You can also add the same element more than once to a List
.So we have implemented just 2 function of list that is insert() and search(). First you insert the digits in list and then search it. We will uploaded more function as soon as possible as like Delete(), Edit() and Update() in next lessons. SOURCE CODE
import java.util.Scanner;
public class ListFunction {
static List<Integer> integer_list = new ArrayList<Integer>();
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
boolean y=true;
System.out.println("**** List in Java ******* ");
do{
System.out.println(" 1 : Insert ");
System.out.println(" 2 : Search ");
System.out.println(" Choice : ");
int choice=input.nextInt();
switch(choice)
{
case 1:
insert();
break;
case 2:
search();
break;
default:
System.out.println("Invalid Input");
break;
}
System.out.println("Want Again (y/n) ? ");
String x=input.next();
char ch=x.charAt(0);
if( ch=='n')
y=false;
}
while(y!=false);
}
public static void insert()
{
Scanner insert=new Scanner(System.in);
boolean y=true;
do{
System.out.println("Enter Number : ");
int num=insert.nextInt();
if(num==0)
System.out.println("Enter Number Greater Than Zero ! ");
else
integer_list.add(num);
System.out.println("Insert Again (y/n) ?");
String next=insert.next();
char ch=next.charAt(0);
if( ch=='n')
y=false;
}
while(y!=false);
}
public static void search()
{
System.out.println("Enter Number for Search = ");
Scanner search=new Scanner(System.in);
int num=search.nextInt();
for(int i=0;i<integer_list.size();i++){
if(integer_list.get(i)==num)
System.out.println(num+" Found in Index : "+i);
}
if(integer_list.lastIndexOf(num)==-1)
System.out.println(num+" Not Present :( ");
}
}
Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.
i think
ReplyDeleteArray List can stores only object references. That's why, it's impossible to use primitive data types like double or int. Use wrapper class (like Integer or Double) instead