330 likes | 346 Views
Learn how to work with arrays in Java through a series of exercises, covering sorting, searching, and other array operations. Practice split, mirror, maximum, and more in object-oriented programming.
E N D
Arrays grades for students in a large course we need a lot of variables double[] grade; grade=new double[numberOfStudents]; creates “variables” grade[0],grade[1],...,grade[numberOfStudents-1]
Arrays double[] grade; grade=new double[numberOfStudents]; creates “variables” grade[0],grade[1],...,grade[numberOfStudents-1] the variables have nice names double sum=0,average; for (int i=0;i<numberOfStudents;i++) sum+=grade[i]; average=sum/numberOfStudentes;
Arrays – another example String[] name; int[] phoneNumber; name=new double[numberOfPeople]; phoneNumber=new int[numberOfPeople]; int findPhoneNumber(String searchedName) { int i=0; while ((i<numberOfPeople)&& (!searchedName.equals(name[i]))) i++; if (i==numberOfPeople) return –1; else return phoneNumber[i]; }
EXERCISE #1: String[] name; int[] phoneNumber; name=new double[numberOfPeople]; phoneNumber=new int[numberOfPeople]; Do we need to search the phone book sequentially?
EXERCISE #2 - OOP: String[] name; int[] phoneNumber; name=new double[numberOfPeople]; phoneNumber=new int[numberOfPeople]; not OOP! What class(es) should we use? Define the class(es).
Arrays as parameters public class Test { public static double average(double[] a) { if (a.length>0) { double sum=0; for (int i=0;i<a.length;i++) sum+=a[i]; return sum/a.length; } } else return 0; } We can figure out the size of the array!
Returning arrays public class Test { public static int[] firstNumbers(int x) { int[] a=new int[x]; for (int i=0;i<x;i++) a[i]=i; return a; } }
EXERCISE #3a: Define a method void mirror(int[] a) { } a = | 6 | 10 | 3 | 2 | 1 | a = | 1 | 2 | 3 | 10 | 6 |
EXERCISE #3b: Define a method int[] mirror(int[] a) { } The method should not modify the input array, but return a mirrored copy a = | 6 | 10 | 3 | 2 | 1 | a = | 1 | 2 | 3 | 10 | 6 |
EXERCISE #4: Define a method int maximum(int[] a) { } a = | 6 | 10 | 3 | 2 | 1 | 10
EXERCISE #5: Define a method int indexOfMaximum(int[] a) { } a = | 6 | 4 | 3 | 2 | 1 | 0 a = | 6 | 10 | 3 | 2 | 1 | 1 a = | 6 | 1 | 3 | 2 | 10 | 4
EXERCISE #6a: Define a method void sort(int[] a) { } a = | 6 | 10 | 3 | 2 | 1 | a = | 1 | 2 | 3 | 6 | 10 |
int indexOfMaximum(int[] a,int length) { int index=0; for (int i=1;i<length;i++) if (a[i]>a[index]) index=i; return index; } void sort(int [] a) { for (int length=a.length;length>0;length--) { int index=indexOfMaximum(a,length); int c=a[length-1]; a[length-1]=a[index]; a[index]=c; } }
EXERCISE #7a: Define a method int split(int[] a,int x) { } 1,9,8,4,5,2,3,8,9,2,3,9,5,3,2,4 and 4 1,4,2,3,2,3,3,2,4,9,8,8,9,9,5 returned index smaller or equal larger
int split(int[] a,int x) { int b=0,e=a.length-1; while (b<=e) { while ((b<=e)&&(a[b]<=x)) b++; while ((b<=e)&&(a[e]>x)) e--; if (b<e) { int c=a[b]; a[b]=a[e]; a[e]=c; } } return b-1; } (in place – solution) INVARIANT = (i<b => a[i]<=x) && (i>e => a[i]>x)
EXERCISE #7b: Define a method int split(int[] a,int x,int start,int end) { } 9,3,5,1,9,8,4,5,2,3,8,9,2,3,9,5,3,2,4,2,3 and 4 9,3,5,1,4,2,3,2,3,3,2,4,9,8,8,9,9,5,2,3 returned index smaller or equal larger
int split(int[] a,int x,int start,int end) { int b=start,e=end; while (b<=e) { while ((b<=e)&&(a[b]<=x)) b++; while ((b<=e)&&(a[e]>x)) e--; if (b<e) { int c=a[b]; a[b]=a[e]; a[e]=c; } } return b-1; } INVARIANT = (i<b => a[i]<=x) && (i>e => a[i]>x)
Can we use split for sorting? void sort(int[] a,int start,int end) { int middle=split(a,a[start],start,end); sort(a,start,middle); sort(a,middle+1,stop); } This never finishes.
Can we use split for sorting? void sort(int[] a,int start,int end) { if (start<end) { int middle=split(a,a[start],start,end); sort(a,start,middle); sort(a,middle+1,end); } } it can be that middle=end or middle+1=start This never finishes, too.
Can we use split for sorting? Solution – modify split so that start<=middle<end. We need to modify the specification too... it can be that middle=end or middle+1=start This never finishes, too.
EXERCISE #7c: + each part has at least one element!!! Define a method int split2(int[] a,int x) { } we are promised that x occurs in a 1,9,8,4,5,2,3,8,9,2,3,9,5,3,2,4 and 4 1,4,2,3,2,3,3,2,4,9,8,8,9,9,5 returned index smaller or equal larger or equal
int split2(int[] a,int x) { int b=0,e=a.length-1; while (true) { while (a[b]>x) b++; while (a[e]<x) e--; if (b<e) { int c=a[b]; a[b]=a[e]; a[e]=c; b++; e--; } else return e; } } INVARIANT: (i<b => a[i]<=x) && (i>e => a[i]>=x) CLAIM: (e>=0)&&(e<a.length-1)
Can we use split for sorting? void sort(int[] a,int start,int end) { if (start<end) { int middle=split2(a,a[start],start,end); sort(a,start,middle); sort(a,middle+1,end); } } it cannot be that middle=end or middle+1=start Now it always finishes.
Can we use split for sorting? void sort(int[] a,int start,int end) { if (start<end) { int middle=split(a,a[start],start,end); sort(start,middle); sort(middle+1,stop); } } a[(int)(Math.random()*(end-start)+start)] QUICKSORT – one of the fastest sorting algorithms!
Array index out of bounds int a=new int[10],b=0; for (int i=0;i<=10;i++) { a[i]=i; b+=a[i]; } System.out.println(“”+b);
Initializing arrays int[] smallPrimes={2,3,5,7,11,13,17,23,29}; int[] smallPrimes; smallPrimes =new int[10]; smallPrimes[0]=2; smallPrimes[1]=3; .... smallPrimes[9]=29;
EXERCISE #8: int[] a = {1,2,3}; int[] b = {4,5}; int[] c; c=a; a=b; b=c; System.out.println("a[0] = "+a[0]); System.out.println("a.length = "+a.length);
EXERCISE #9: void swap(int[] d,int []e) { int[] f=d; d=e; e=f; } int[] a = {1,2,3}; int[] b = {4,5}; int[] c; swap(a,b); System.out.println("a[0] = "+a[0]);
EXERCISE #10: void swap(int[] d,int []e) { int f=d[0]; d[0]=e[0]; e[0]=f; } int[] a = {1,2,3}; int[] b = {4,5}; int[] c; swap(a,b); System.out.println("a[0] = "+a[0]); System.out.println("a[1] = "+a[1]);
EXERCISE #11: void swap(int d,int e) { int f=d; d=e; e=d; } int[] a = {1,2,3}; int[] b = {4,5}; int[] c; swap(a[0],b[0]); System.out.println("a[0] = "+a[0]); System.out.println("a[1] = "+a[1]);
Difference from C++ class Test { private int data; public Test(int data) { this.data=data; } public int GetValue() { return data; } } Test[] a; a=new Test[10]; a[0]=new Test(3); System.println(a[0].GetValue()); System.println(a[1].GetValue()); error!
Multidimensional Arrays grades for students in a large course, want to keep grade for each assignment separately double[][] grade; grade=new double[numberOfStudents][numberOfHomeworks]; Compute the average from i-th homework? Compute the sum for each student?
Multidimensional Arrays double[][] grade; grade=new double[numberOfStudents][numberOfHomeworks]; double[][] grade; grade=new double[numberOfStudents][]; for (int i=0;i<numberOfStudents;i++) grade[i]=new double[numberOfHomeworks];