Lecture 4 - Arrays Flashcards
How do you expand a fixed array?
- Check if there is room (if pos>array.length)
- If there is no room, create a new length variable which is the array.length + 5 (num needed to add)
- Create a new array with this length
- Drop all of the values from the old array into the new array using a for loop
- Reset the reference of the old array that was too small to the new array
How do you sort a fixed array?
Arrays.sort()
Ie., Arrays.sort(grades) sorts the grades in an array
What is the java string compareTo() method
It is a method compares the given string with current string lexicographically. It returns positive number, negative number or 0.
public static void main(String[] args){
String s1=”hello”;
String s2=”hello”;
String s3=”meklo”;
String s4=”hemlo”;
String s5=”flag”;
System.out.println(s1.compareTo(s2));//0 because both are equal
System.out.println(s1.compareTo(s3));//-5 because “h” is 5 times lower than “m”
System.out.println(s1.compareTo(s4));//-1 because “l” is 1 times lower than “m”
System.out.println(s1.compareTo(s5));//2 because “h” is 2 times greater than “f”
In the compareTo() how do you compare string objects or ints
For Strings we use the compareTo()
ie., If our String is less than the other String return -1
if(this.s1.compareTo(l.getS1())<0) return -1;
For ints we just use < >
If this age is greater than the other age return 1
if(this.age > l.getAge()) return 1;
I have extended the ExpandableArray class to another class ExpandFixedSizedArray which implements the Comparable interface. My compareTo method is as follows.
Study the method and explain in words how I am comparing two elements of this class.:
public int compareTo(Object o) {
if (o instanceof A) {
A other = (A) o;
if (this.getLength() > other.getLength()) return 1;
if (other.getLength() > this.getLength()) return -1;
// if we get here we know they are the same length for (int i = 0; i < this.getLength(); i++) { if (myArray[i] > other.myArray[i]) return 1; if (myArray[i] < other.myArray[i]) return -1; } return 0; } return 0; }
If the lengths differ, the one with the smaller length is assumed to be smaller. If the lengths are the same then the individual elements of the array are compared. If the arrays are not identical then the elements at the first position at which the two arrays differ are compared. Otherwise the two arrrays are the same and zero is returned.
How do we print the elements of an int array [1,2,3,4] like this 1,2,3,4
Loop over the array.length-1 and set s to be each element as a string with each a comma after it (1,2,3) and then add the final element to the String s. (4)
public static String printArray(int[] a){ String s = ""; for(int i = 0 ; i < a.length-1; i++) s = s + a[i] + ", " ; s = s + a[a.length-1]; return s; }}
How do we print all the elements from an object array?
public static String printArray(Avatar[] avatars) {
String s = “”;
int n = 0;
while (avatars[n] != null && n <= avatars.length - 1) n++;// there are n legitimate entries if (n > 1) { for (int i = 0; i < n-1; i++) s += avatars[i] + ",\n"; s += avatars[n - 1];} return s; }
Any main method in a class is declared as follows: public static void main(String[] args) { } What type of parameter(s) is it expecting?
it’s expecting an array of elements of type String (possibly of length 0 or might have something in it)
Write a method that says we are going to pass it an array of numbers, but we’re not sure how many yet?
public static int sum(int…numbers){
int sum=0;
for(int i = 0; i
Write a method that we will pass an array of objects, but we’re not sure how many yet
public static int totalRiches(Avatar... players) { int sum = 0; for (int i = 0; i < players.length; i++) sum += players[i].getGold(); return sum; }