Fixed arrays are often used in many standard problems. We will examine the most common use.
First some data to work with:
- To define and initialize a fixed array A with 5 chars with A[0] = 't' and A[4] = 'm':
private char[] A = { 't', 'p', 'd', 'a', 'm' };
- To define and initialize a fixed array B with 5 Strings with B[0]="tomato" and B[4]="mango":
private String[] B = {"tomato", "pear", "durian", "apple", "mango"};
- To compare two strings, essentially "apple" < "mango", the following prints the alphabetically smaller of the two, "apple":
if( "apple".compareTo("mango") < 0)
System.out.println("apple");
else
System.out.println("mango");
|
A |
B |
|
|
Define Initialize |
public class ExampleA { private char[] A = { 't', 'p', 'd', 'a', 'm' }; OR |
public class ExampleB public class ExampleB |
| public void print( ) { for( int i=0; i < A.length; i++) System.out.println( A[ i ] ); } |
public void print( ) { for( int i=0; i < B.length; i++) System.out.println( B[ i ] ); } |
|
| Find Return |
public int find( char s ) { for (int i=0; i < A.length; i++) if( A[ i ] == s ) return i; return -1; |
public int find( String s ) { for (int i=0; i < B.length; i++) if( B[ i ].equals( s ) ) return i; return -1; |
| Minimum |
public char minimum()
{ char min = A[0]; for (int i=0; i < A.length; i++) if( A[ i ] < min ) min = A[ i ]; return min; |
public String minimum()
{ String min = B[0]; for (int i=0; i < B.length; i++) if( B[ i ].compareTo( min ) < 0 ) min = B[ i ]; return min; |
| Index of Minimum |
public int indexOfMinimum( )
{ int index = 0; for (int i=0; i < A.length; i++) if( A[ i ] < A[ index ] ) index = i; return index; |
public int indexOfMinimum( )
{ int index = 0; for (int i=0; i < B.length; i++) if( B[ i ].compareTo( B[ index ] ) < 0 ) index = i; return index; |
| Total
|
public String total( )
{ String result = ""; for (int i=0; i < A.length; i++) result = result + A[ i ]; return result; |
public String total( )
{ String result = ""; for (int i=0; i < B.length; i++) result = result + B[ i ]; return result; |
| Maximum at End |
public void maximumAtEnd()
{
for(int j=0; j < A.length - 1; j++) if( A[ j ] > A[ j+1 ]
) A[ j ]=A[ j+1 ]; A[ j+1 ]=temp; } |
public void maximumAtEnd() {
for(int j=0; j < B.length - 1; j++) if( B[ j ].compareTo( B[ j+1 ] ) >
0 ) B[ j ] = B[ j+1 ]; B[ j+1 ] = temp; } |
| Sort |
public void sort()
{
for(int pass=1; pass < A.length; pass++) for(int j=0; j < A.length - pass; j++) if( A[ j ] > A[ j+1 ]
) A[ j ]=A[ j+1 ]; A[ j+1 ]=temp; } |
public void sort()
{
for(int pass=1; pass < B.length; pass++) for(int j=0; j < B.length-pass; j++) if( B[ j ].compareTo( B[ j+1 ] ) >
0 ) B[ j ] = B[ j+1 ]; B[ j+1 ] = temp; } |
Label each Cup 0, 1, 2, 3 and temp.
Put obviously different volumes of candy in Cup 0-3.
Arrange Cup array in numerical order: 0, 1, 2, 3

Hand execute the following algorithm using your real Candy cup array.
Always keep the the cups in 0-3 order.
Follow the algorithm, pouring cup contents.
| for( int j=0; j < cup.length - 1; j++) if( cup[ j ] >
cup[ j+1 ]
) cup[ j ]=cup[ j+1 ]; cup[ j+1 ]=temp; } |
Continue with the cup array from above. Hand execute the sort algorithm below.
| public void sort()
{
for(int pass=1; pass < cup.length; pass++) for(int j=0; j < cup.length - pass; j++) if( cup[ j ] >
cup[ j+1 ]
) cup[ j ]=cup[ j+1 ]; cup[ j+1 ]=temp; } |
- Write the value of pass and j.
pass j 1
0 Follow the algorithm, pouring cup contents.
Always keep the the cups in 0-3 order.
- When finished the candy should be sorted by volume.
- Now eat some candy.
We assumed Candy class and cup fixed array existed.
Also that if( cup[ j ] > cup[ j+1 ] ) worked.
What are the actual definitions required?
| public class Candy { private int volume; public Candy( int volume ) public int getVolume()
|
public class CandyLand { Candy cup[ ] = { new Candy( 40 ) , new Candy( 80 ), new Candy( 20 ),
new Candy( 60 ) for(int pass=1; pass < cup.length; pass++) for(int j=0; j < cup.length - pass; j++) if( cup[ j ].getVolume() >
cup[ j+1 ].getVolume()
) cup[ j ]=cup[ j+1 ]; cup[ j+1 ]=temp; } |

Hand execute the sort while redrawing the object diagram.

| pass | j | A[ j ] | A[ j+1 ] | temp | A='t', 'p', 'd', 'a', 'm' |
| 1 | 0 | 't' | 'p' |
| pass | j | B[ j ] | B[ j+1 ] | temp | B="tomato", "pear", "durian", "apple", "mango" |
| 1 | 0 | "tomato" | "pear" |
Given the definitions below, give the Java to:
int list[] = { -4, -3, -6, -1, -7, -20, 15, -2, 27, -14, -18, 4, -5 };
String names[] = { "Brad", "C. G.", "Michael", "Joe", "Kyle" };
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 "Nancy".
| 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 size() of names.
| int middle = names.length / 2; System.out.println( names[ middle ] ); |
reverse the values so that list[0] is 4, list[1] is -18, etc. Hint: while may be simpler than a for statement.
| int l = 0; int r = list.length-1; int temp; while( l < r ) { temp = list[ l ]; list[ l ] = list[ r ]; list[ r ] = temp; l = l + 1; r = r - 1; } |
reverse the values so that names[0] is "Kyle", names[1] is "Joe", 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; } |