Java Array

Sang Shin, sang.shin@sun.com, Sun Microsystems, www.javapassion.com/javaintro


In this lab, you are going to build and run sample applications that Java array.


Expected duration: 90 minutes


Software Needed

Before you begin, you need to install the following software on your computer.


Change Log



Lab Exercises


Exercise 1: Build and run Java programs that use Java array of int

In this exercise, you are going to build  simple Java applications that use  array  of int.

  1. Build and run a Java program that uses single-dimensional array of int
  2. Build and run a Java program that uses two-dimensional array of int
  3. Build and run a Java program that computes the greatest number

(1.1) Build and run a Java program that uses single-demensional array of int

1. Create a NetBeans project


Figure-1.10: Create a new NetBeans project
2. Modify the IDE generated JavaArray.java.
public class JavaArray {
   
    /** Creates a new instance of JavaArray */
    public JavaArray() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // Declare and create int array whose size is 10
        int[] ages = new int[10];
       
        // Display the value of each entry in the array
        for( int i=0; i<ages.length; i++ ){
            System.out.print( ages[i] );
        }
    }
   
}
Code-1.11: Modified JavaArray.java

3. Build and run the program

0 0 0 0 0 0 0 0 0 0
Figure-1.12: Result

4. (For your own exercise) Modify JavaArray.java as following, build and run the program.


(1.2) Build and run a Java program that uses two-dimensional array of int

1. Create a NetBeans project

2. Modify the IDE generated JavaTwoDimensionArray.java.
public class JavaTwoDimensionArray {
   
    /** Creates a new instance of JavaTwoDimensionArray */
    public JavaTwoDimensionArray() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // Declare and create two dimensional int array whose size is 10 by 5
        int[][] ages = new int[10][5];
       
        // Display the number of rows and columns
        System.out.println("ages.length = " + ages.length);
        System.out.println("ages[1].length = " + ages[1].length);
       
        // Display the value of each entry in the array
        for( int i=0; i<ages.length; i++ ){
             System.out.println("\nStarting row " + i);
            for( int j=0; j<ages[i].length; j++ ){
                ages[i][j] = i * j;
                System.out.print( ages[i][j] + " " );
            }
        }
    } 
}
Code-1.11: Modified JavaTwoDimensionArray.java

3. Build and run the program

ages.length = 10
ages[1].length = 5

Starting row 0
0 0 0 0 0
Starting row 1
0 1 2 3 4
Starting row 2
0 2 4 6 8
Starting row 3
0 3 6 9 12
Starting row 4
0 4 8 12 16
Starting row 5
0 5 10 15 20
Starting row 6
0 6 12 18 24
Starting row 7
0 7 14 21 28
Starting row 8
0 8 16 24 32
Starting row 9
0 9 18 27 36
Figure-1.12: Result

4. (For your own exercise) Create a NetBeans project as following.  Build and run the program.

(1.3) Build and run a Java program that finds the greatest number

1. Create a NetBeans project

2. Modify the IDE generated GreatestNumber.java.
import javax.swing.JOptionPane;

public class GreatestNumber {
   
    public static void main(String[] args) {
        int[] num = new int[10];
        int counter;
        int max = 0;
        int totalnumber = 3;
       
        // Prompt a user to enter numbers
        for(counter = 0; counter < totalnumber; counter++){
           
            num[counter] = Integer.parseInt
                    (JOptionPane.showInputDialog("Enter numbers until " + totalnumber + " numbers are entered"));
           
            // Compute the greatest number up to this point
            if((counter == 0)||(num[counter] > max))
                max = num[counter];
        }
       
        // Display the greatest number.
        JOptionPane.showMessageDialog(null,"The number with the greatest value is " + max);
    }
   
}
Code-1.11: Modified GreatestNumber.java

3. Build and run the program

4. (For your own exercise) Modify MyGreatestNumberProject as following:

Summary

In this exercise, you learned how to use Java array.

                                                                                                                        return to the top



Exercise 2: Build and run Java programs that use Java array of String

In this exercise, you are going to build  simple Java applications that use  array  of String.

  1. Build and run a Java program that uses single-dimentional array of String

(2.1) Build and run a Java program

1. Create a NetBeans project

2. Modify the IDE generated DaysOfTheWeek.java.
public class DaysOfTheWeek {
    
    public static void main(String[] args) {
       
        // Declare and initialize String array of the days of the week
        String[] days = {"Sunday","Monday","Tuesday","Wednesday",
        "Thursday","Friday","Saturday"};
       
        // Display days of the week using while loop
        System.out.println("Display days of week using while loop");
        int counter = 0;
        while(counter < days.length){
            System.out.println(days[counter]);
            counter++;
        }
       
        // Display days of the week using do-while loop
        System.out.println("Display days of week using do-while loop");
        counter = 0;
        do{
            System.out.println(days[counter]);
            counter++;
        } while(counter < days.length);
       
        // Display days of the week using for loop
        System.out.println("Display days of week using for loop");
        for(counter = 0; counter < days.length; counter++){
            System.out.println(days[counter]);
        }
       
    }
   
}
Code-2.11: Modified DaysOfTheWeek.java

3. Build and run the program


Display days of week using while loop
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Display days of week using do-while loop
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Display days of week using for loop
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Figure-2.12: Result


Homework exercise (for people who are taking Sang Shin's "Java Programming online course")


1. The homework is to create the MyOwnJavaArrayProject project as following:
Hint: You will have use various methods of String class.  The following is an example code fragment you might be able to use.  Please feel free to use alternative ways of doing the homework.

// Get the first name from a name string using split() instance (non-static) method
String[] nameArrayForPerson1 = person1NameInstance.split(" ");
String[] nameArrayForPerson2 = person2NameInstance.split(" ");

// Get the lengths of strings using length() instance (non-static) method
int lengthOfFirstNameOfPerson1 = nameArrayForPerson1[0].length();
int lengthOfFirstNameOfPerson2 = nameArrayForPerson2[0].length();

// Compare the lengths of the first names between person1 and person2
if (lengthOfFirstNameOfPerson1 > lengthOfFirstNameOfPerson2){
   System.out.println(nameArrayForPerson1[0] +
                              " has longer first name than " +
                              nameArrayForPerson2[0]);
}


2. Send the following files to javaprogramminghomework@sun.com with Subject as JavaIntro-javaarray.