INNOVATION
Here you find different learning things from my side about Computer Science
Saturday, 8 August 2015
ArrayList in java
ArrayList
Here we are going to implement List program with help of ArrayList Algorithms
Main function should be Add_At_Position,Delete_From_Position,Display,GetSize,Sorting,etc.
import java.util.*; class ArrayList { private int Item[]; private int position; private int SIZE; public ArrayList(int no)//parametrized contractor { position=-1; SIZE=no; Item=new int[no]; } public void FirstAdd(int text)//For add member at first { position++; Item[position]=0; int j; for(int i=position;i>=0;i--) { if(i!=0) { j=i; int temp=Item[j]; Item[j]=Item[j-1]; Item[j-1]=temp; } else { Item[i]=text; System.out.println("\nSuccessfully added.\n"); } } } public void LastAdd(int text)//For member at last { position++; Item[position]=text; System.out.println("\nSuccessfully added.\n"); } public void PositionAdd(int text)//for add member at given position { Scanner x=new Scanner(System.in); System.out.println("\nThere are this many list in List :- "+(++position)); System.out.print("\nAT which position you want to add :- "); int pos=x.nextInt(); pos--; int j; for(int i=position;i>=pos;i--) { if(i!=pos) { j=i; int temp=Item[j]; Item[j]=Item[j-1]; Item[j-1]=temp; } else { Item[i]=text; System.out.println("\nSuccessfully added.\n"); } } } public void Display()//display data function { System.out.println("\n\n\nLIST OF MEMBERS\n\n\n"); for(int i=0;i<=position;i++) { int k=i;k++; System.out.println(k+" :- "+Item[i]); } } public int Search(int text)//search by value { for(int i=0;i<=position;i++) { if(text==Item[i]) { return i+1; } } return -1; } public int Searchpos(int pos)//search by position { pos--; if(pos<=position) return Item[pos]; else return -1; } public void Delete(int number,int pos)//delete function { pos--; if(number==0) { position--; System.out.println("\nSuccessfully Deleted.\n"); return; } else if(number==(SIZE-1)) { position--; System.out.println("\nSuccessfully Deleted.\n"); return; } else { for(int i=pos;i
Item[j]) { int Temp=Item[i]; Item[i]=Item[j]; Item[j]=Temp; } } } } public int Getpos() { return position; } }
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment