Chapter 4 and 5 - Array and ArrayList comparison examples

The following are similar solutions using array and ArrayList objects. Three points of note:

  1. 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'    };
     

  2. 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"};
     

  3. 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");
     

Define

Array

public class arrayExample
{
   private char[] A = { 't', 'p', 'd', 'a', 'm'  };

   private String[] B;

   public arrayExample()
   { 
       B = new String[5];

       B[0] = "tomato";
       B[1] = "pear";
       B[2] = "durian";
       B[3] = "apple";
       B[4] = "mango";
   }

ArrayList

import java.util.ArrayList;

public class arrayListExample
 {
   private ArrayList <String> B ;

   public arrayListExample()
   {
      B = new ArrayList <String> ()

      B.add("tomato");
      B.add("pear");
      B.add("durian");
      B.add("apple");
      B.add("mango");
   }

Print    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
index
where
String s
found
or -1
if not

   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(B.get( i ).equals( s ) )
            return i;
      return -1;
   }

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 = B.get(0);
   for (int i=0; i<B.size(); i++)
       if( B.get(i).compareTo(least) < 0 )
            least = 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( B.get( i ).compareTo( B.get( least ) ) < 0 )
            least = i;
   return least;
}

Sort
char
array
 
public void 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;
         }
}
 

   ArrayList is a collection of objects,

   no primitives such as char, int, boolean

 


Sort
String
 
public void bubbleSort()
{
   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)
         {
               String temp=B[ j ];
               B[ j ] = B[ j+1 ];
               B[ j+1 ] = temp;
         }
}
public void bubbleSort()
{
   for(int pass=1; pass < B.size(); pass++)
     for(int j=0; j<B.size()-pass; j++)
        if( B.get(j).compareTo( B.get(j+1) ) < 0)
        {
               String temp=B.get( j );
               B.set( j, B.get( j+1 ));
               B.set( j+1 , temp );
         }
}

Practice

  1. Hand sort fixed array A:

    char[] A = { 't', 'p', 'd', 'a', 'm'  };

    using the bubbleSort() method by completing the following table:
     
    pass j A[ j ] A[ j+1 ] temp A='t', 'p', 'd', 'a', 'm'
    1 0 't' 'p'    


     

  2. Hand sort fixed array B:

    String [] B = {"tomato", "pear", "durian", "apple", "mango"};

    using the bubbleSort() method by completing the following table:
     
    pass j B[ j ] B[ j+1 ] temp B="tomato", "pear", "durian", "apple", "mango"
    1 0 "tomato" "pear"    


     

 

Given the definitions, 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" };

  1. set all the variables of list array to 0.

    for(int i=0; i<list.length; i++) list[i] = 0;
  2. 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;
  3. set all the variables of names array to null.

    for(int i=0; i<names.length; i++) names[i] = null;
  4. set a random variable (0 to 4) of names array to "Nancy".

    Random rN = new Random();
    names[ rN.nextInt(5) ] = "George";
  5. 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++;
  6. total the variables of list array.

    int total;
    for(int i=0; i<list.length; i++)
          total = total + list[ i ];
  7. concatenate the variable of names array into a single String.

    String single = "";
    for(int i=0; i<names.length; i++)
        single = single + names[ i ];
  8. 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 ] );
  9. 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 ] );
  10. 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;
    }
  11. 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;
    }