Sie sind auf Seite 1von 20

Tutorial 4

Prepared by : Ng Mee Mee


Learning Objectives

 Introducing Arrays
 Declaring Array Variables, Creating Arrays,
and Initializing Arrays
 Passing Arrays to Methods
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data.
The entire array Each value has a numeric index
has a single name

0 1 2 3 4 5 6 7 8 9

scores 79 87 94 82 67 98 87 81 74 91

An array of size N is indexed from zero to N-1

This array holds 10 values that are indexed from 0 to 9


Introducing Arrays, cont
 A particular value in an array is referenced using the array name
followed by the index in brackets
 For example, the expression

scores[2]

refers to the value 94 (the 3rd value in the array)


 That expression represents a place to store a single integer and
can be used wherever an integer variable can be used
Introducing Arrays,cont
 For example, an array element can be assigned a
value, printed, or used in a calculation:
scores[2] = 89;
scores[first] = scores[first] + 2;
mean = (scores[0] + scores[1])/2;
System.out.println ("Top = " +
scores[5]);
Introducing Arrays,cont
 The values held in an array are called array elements
 An array stores multiple values of the same type (the element
type)
 The element type can be a primitive type or an object reference
 Therefore, we can create an array of integers, or an array of
characters, or an array of String objects, etc.
 In Java, the array itself is an object
 Therefore the name of the array is a object reference variable,
and the array itself must be instantiated
Declaring Array Variables
 Syntax to declare an array varaible:
 dataType[] arrayName;
Or
 dataType arrayName[];//this style is
correct , but not preferred

Example:
double[] myList;
or
double myList[];
Creating Arrays
 Using new operator to create an array
arrayName = new dataType[arraySize];
 It creates an array using new dataType[arraySize]

 Ir assigns the reference of the newly created array to the


variable arrayName

 Example:
double[] myList = new double[10];

 myList[0] references the first element in the array.


 myList[9] references the last element in the array.
Creating Arrays, cont

double[] myList = new double[10];

myList reference myList[0] 5.6


myList[1] 4.5
myList[2] 3.3 Element value
Array myList[3] 13.2
reference myList[4] 4
variable myList[5] 34.33
myList[6] 34
Array element at myList[7] 45.45
index 5 myList[8] 99.993
myList[9] 11123
Declaring and Creating
in One Step
 datatype[] arrayname = new
datatype[arraySize];
double[] myList = new double[10];

 datatype arrayname[] = new


datatype[arraySize];
double myList[] = new double[10];
The Length of Arrays
 Once an array is created, its size is fixed. It
cannot be changed. You can find its size
using

arrayVariable.length

For example,
myList.length returns 10
Example: The Length of
Arrays
public class ArrayLength{
public static void main(String[] args){
double[ ] myList=new double[10];
System.out.println(myList.length);
System.out.println(myList[9]);
}
}
Initializing Arrays
 Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] = i;

 Declaring, creating, initializing in one step:


double[] myList = {1.9, 2.9, 3.4, 3.5};

This shorthand syntax must be in one statement.


Example: Initializing Arrays Using a for
loop
public class ArrayLength{
public static void main(String[] args){
double[ ] myList=new double[10];

for(int i=0; i < myList.length; i++){


myList[i] =i;
System.out.println(myList[i]);
}
System.out.println("the last element of myList is
" + myList[9]);
}
}
Example: Declaring, creating, initializing in one
step
public class ArrayLength{
public static void main(String[] args){
double[ ] myList={1.9,2.9,3.4,3.5};
for(int i=0; i < myList.length; i++){
System.out.println(myList[i]);

}
}
}
Declaring, creating, initializing
Using the Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5};

This shorthand notation is equivalent to the


following statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
Example: Declares, creates, and initializes
the array myList with 4 elements
public class ArrayLength{
public static void main(String[] args){
double[] myList=new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
for(int i=0; i < myList.length; i++){
System.out.println(myList[i]);

}
}
}
Passing Arrays to Methods
  Java uses pass by value to pass parameters 
to a method. There are important 
differences between passing a value of 
variables of primitive data types and 
passing arrays.
  For a parameter of a primitive type 
value, the actual value is passed. 
Changing the value of the local parameter 
inside the method does not affect the 
value of the variable outside the method.
  For a parameter of an array type, the 
value of the parameter contains a 
reference to an array; this reference is 
passed to the method. Any changes to the 
array that occur inside the method body 
Example: Passing Arrays to
Methods
public class ArrayLength{
public static void main(String[ ] args){
double[ ] myList=new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
printArray(myList);
}
public static void printArray(double[ ] myList){
for(int i=0; i < myList.length; i++){

System.out.println(myList[i]);

}
}
}
Example: Pass by Value
public class Test{
public static void main(String[] args){
int x =1; // x repersents an int values
int[] y = new int[10]; // y represents an array of int values

m(x,y); //invoke m with argunment x and y


System.out.println("x is " + x);
System.out.println("y[0] is " + y[0]);
}
public static void m(int number, int[] numbers){
number=1001;//assign a new value to number
numbers[0]=5555;//assign a new value to numbers[0]

}
}

Das könnte Ihnen auch gefallen