The following are similar solutions using array and ArrayList objects. Three points of note:
private char[] A = { 'l', 'p',
'd', 'a', 'o' };
private String[] B = {"lychee", "pear",
"durian", "apple", "orange"};
if( "apple".compareTo("orange") < 0)
System.out.println("apple");
else
System.out.println("orange");
Define |
Arraypublic class arrayExample |
ArrayListimport java.util.ArrayList; |
| public void printArray() { for (int i=0; i<B.length; i++) System.out.print(" " + B[ i ]); } |
public void printArrayList()
{ for (int i=0; i<B.size(); i++) System.out.println( B.get( i ) ); } |
|
| String Search Return |
public int searchString(String s) { for (int i=0; i<B.length; i++) if(((String)B[ i ]).equals( s ) ) return i; return -1; } |
public int searchString(String s) { for (int i=0; i<B.size(); i++) if(((String)B.get( i )).equals( s ) ) return i; return -1; } |
| Return Least char |
public char leastChar() { char least = A[0]; for (int i=0; i<A.length; i++) if(A[ i ] < least ) least = A[ i ]; return least; } |
ArrayList is a collection of objects, no primitives such as char, int, boolean |
| Least String |
public String leastString() { String least = B[0]; for (int i=0; i<B.length; i++) if(B[ i ].compareTo(least) < 0 ) least = B[ i ]; return least; } |
public String leastString() { String least = (String)B.get(0); for (int i=0; i<B.size(); i++) if(((String)B.get(i)).compareTo(least) < 0 ) least = (String)B.get( i ); return least; } |
| Index of least String |
public int indexOfleastString()
{ int least = 0; for (int i=0; i<B.length; i++) if(B[ i ].compareTo(B[ least ]) < 0 ) least = i; return least; } |
public int indexOfleastString()
{ int least = 0; for (int i=0; i<B.size(); i++) if(((String)B.get( i )).compareTo((String)B.get( least )) < 0 ) least = i; return least; } |
| Sort char array ascending |
public char[] bubbleSort() { for(int pass=1; pass < A.length; pass++) for(int j=0; j < A.length-pass; j++) if(A[ j ] > A[ j+1 ]) { char temp=A[ j ]; A[ j ]=A[ j+1 ]; A[ j+1 ]=temp; } return A; } |
ArrayList is a collection of objects, no primitives such as char, int, boolean
|
| Sort String descending |
public String[] bubbleSort() { for(int pass=1; pass < B.length; pass++) for(int i=0; i < B.length-pass; i++) if(B[ i ].compareTo(B[ i+1 ]) > 0) { String temp=B[ i ]; B[ i ] = B[ i+1 ]; B[ i+1 ] = temp; } return B; } |
public ArrayList bubbleSort() { for(int pass=1; pass < B.size(); pass++) for(int j=0; j<B.size()-pass; j++) if(((String)B.get(j)).compareTo((String)B.get(j+1)) < 0) { String temp=(String)B.get( j ); B.set( j, B.get( j+1 )); B.set( j+1 , temp ); } return B; } |
Given the definitions, give the Java to:
int list[] = { -4, -3, -6, -1, -7, -20, 15, -2, 27, -14, -18, 4, -5 };
String names[] = { "Josh", "Michelle", "Michael", "Tom", "Jason" };
set all the variables of list array to 0.
| for(int i=0; i<list.length; i++) list[i] = 0; |
set all the variables of list array to a random number between -10 and 10.
| Random rN = new Random(); for(int i=0; i<list.length; i++) list[i] = rN.nextInt(21)-10; |
set all the variables of names array to null.
| for(int i=0; i<names.length; i++) names[i] = null; |
set a random variable (0 to 4) of names array to "George".
| Random rN = new Random(); names[ rN.nextInt(5) ] = "George"; |
count the number of variables of list array less than 0.
| int count = 0: for(int i=0; i<list.length; i++) if( list[i] < 0 ) count++; |
total the variables of list array.
| int total; for(int i=0; i<list.length; i++) total = total + list[ i ]; |
concatenate the variable of names array into a single String.
| String single = ""; for(int i=0; i<names.length; i++) single = single + names[ i ]; |
print the value of the variable in the middle of array list using the length of list.
| int middle = list.length / 2; System.out.println( list[ middle ] ); |
print the value of the variable in the middle of array names using the length of names.
| int middle = names.length / 2; System.out.println( names[ middle ] ); |
reverse the list order so that list[0] is -5, list[1] is 4, etc. Hint: while may be simpler than a for statement.
| int b = 0;
// beginning array index int e = list.length-1; // end array index int temp; while( b < e ) { temp = list[ b ]; list[ b ] = list[ e ]; list[ e ] = temp; b = b + 1; e = e - 1; } |
reverse the values so that names[0] is "Jason", names[1] is "Tom", etc.
| int l = 0; int r = names.length-1; String temp; while( l < r ) { temp = list[ l ]; list[ l ] = list[ r ]; list[ r ] = temp; l = l + 1; r = r - 1; } |