Array is an object that holds several values of the same type – primitives or objects. Let’s suppose that your program is going to store the names of four classmates in java class. Instead of declaring four different String variables, you can declare one String array that has four elements. Arrays are marked by placing square brackets either after the variable name, or after the data type:

String [] students;


String students[];

These declarations just tell the Java compiler that you are planning to store several text strings in the array students. Each element has its own index (serial number) starting from zero. The next sample actually creates an instance of an array that can store four String elements and assigns the values to the elements of this array:

students= new String [4];

students[0] = "Joshua";
students[1] = "Isaac";
students[2] = "Raphel";
students[3] = "Erik";

In the next part of code, we are doing:

  1. display one of elements in the array
  2. return the size of the array
  3. loop all elements in the array


* @author yzhu
public class ArrayTools {

public String[] getArrayStudents(){
String[] arrayStudents = new String[5];

arrayStudents[0] = “Joshua”;
arrayStudents[1] = “Isaac”;
arrayStudents[2] = “Raphel”;
arrayStudents[3] = “Erik”;

return arrayStudents;


* @param thisArray
* @return the size of thisArray
public int getSize(String[] thisArray){
return thisArray.length;

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub

ArrayTools aTools = new ArrayTools();

String[] javaStudents;
javaStudents = aTools.getArrayStudents();

// you can change the array serial number to print your name
System.out.println(“My name is: ” + javaStudents[0]);

//return size of array
int arraySize;
arraySize = aTools.getSize(javaStudents);
System.out.println(“The size of javaStudents array is: ” + arraySize);

//loop all units in the array
for(int i=0; i<arraySize; i++){
System.out.println(“javaStudent # “+ i +”:” + javaStudents[i]);

//for you work:
// if number i is even number, print the name out
for(int i=0; i<arraySize; i++){
//you might have to add an if precondition to be here
System.out.println(“javaStudent # “+ i +”:” + javaStudents[i]);




Don’t forget to change the last part // for your work, you are required to display the students’ name if his/her serial number in the array is an even number. Why? Because today, even number is a lucky number! LOL


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s