Documente Academic
Documente Profesional
Documente Cultură
ICT1242
Data Structures
//Array public class array { public static void main(String args[]) { int[] myArray=new int[10]; myArray[3]=6; System.out.println("The forth element of the array is\t:"+myArray[3]); System.out.println("Length of the array is\t:"+myArray.length); for(int i=0;i<10;i++) { System.out.print(myArray[i]+" "); } } } /* The forth element of the array is :6 Length of the array is :10 0 0 0 6 0 0 0 0 0 0 */ //Array2 import java.util.*; public class array2 { public static void main(String args[]) { int[] myArray=new int[10]; int element; boolean find; Scanner temp=new Scanner(System.in); for(int i=0;i<10;i++) { System.out.print("Enter a number\t:"); myArray[i]=temp.nextInt(); } int i=0; do { System.out,println("Enter a number to find\t:") element=temp.nextInt(); i++; for(int i=0;i<10;i++)
Madusanka Premaratne ICT1242 { if(element==myArray[i]) find=true; else find=false; } if(find==true) System.out,println("Found..!"); else System.out,println("Try Again..!"); } while(i<2); } } //Array3
Data Structures
import java.util.*; public class array3 { public static void main(String args[]) { int[][] myArray=new int[2][2]; Scanner temp=new Scanner(System.in); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { System.out.print("Enter a number\t:"); myArray[i][j]=temp.nextInt(); } } for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { System.out.println("myArray["+(i+1)+"]["+ (j+1)+"]\t:"+myArray[i][j]); } } } }
ICT1242
Data Structures
import java.util.*; public class arrayStu { public static void main(String args[]) { double[] stuArray=new double[10]; double tot=0; Scanner temp=new Scanner(System.in); for(int i=0;i<10;i++) { System.out.print("Enter the marks\t:"); stuArray[i]=temp.nextInt(); tot+=stuArray[i]; } System.out.println("Total mark is\t:"+tot); System.out.println("Average is\t:"+tot/10); } } /* Enter the marks Enter the marks Enter the marks Enter the marks Enter the marks Enter the marks Enter the marks Enter the marks Enter the marks Enter the marks Total mark is :10.0 Average is :1.0 Process Exit...*/ :1 :1 :1 :1 :1 :1 :1 :1 :1 :1
//LinkedList public class linkedList { int list[]; int front,size,element; public linkedList(int s)
Madusanka Premaratne { size=s; list=new int[size]; front=3; } public void addElt(int elt) { element=elt;
ICT1242
Data Structures
if (front<size) { list[front]=element; front++; } else System.out.println("List is full"); } public void printList() { for(int i=0;i<list.length;i++) { System.out.println(list[i]+" "); } } } class List { public static void main(String args[]) { linkedList l=new linkedList(9); l.addElt(90); l.addElt(92); l.addElt(95); l.addElt(97); l.addElt(98); l.printList(); } }
//Stack //MaDu
Madusanka Premaratne public class stackCheck { int stack[]; int size,top=-1,element; stackCheck(int s) { size=s; stack=new int[size]; //bottom=0; }
ICT1242
Data Structures
public void push(int n) { element=n; if (top<size) { top++; stack[top]=element; } else System.out.println("Stack is Full"); } int pop() { return stack[top--]; } public void printStack() { for(int i=0;i<stack.length;i++) { System.out.println(stack[i]+" "); } } } class Stack { public static void main(String args[]) { stackCheck s=new stackCheck(5); s.push(12); s.push(13);
Madusanka Premaratne ICT1242 s.push(15); s.push(18); s.push(17); s.pop(); s.pop(); s.pop(); s.printStack(); System.out.println("* New Stack *"); s.push(20); s.push(35); s.push(40); //s.push(100); s.printStack(); } } /* output : 12 13 20 35 40 */
Data Structures