0% found this document useful (0 votes)
45 views

Concept of Arrays

An array is a collection of values of the same type. Each item in an array is called an element, which is accessed by its index position. Arrays can hold primitive data types or objects, and their size is fixed when created. Two-dimensional arrays can also be used, like a table with rows and columns.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Concept of Arrays

An array is a collection of values of the same type. Each item in an array is called an element, which is accessed by its index position. Arrays can hold primitive data types or objects, and their size is fixed when created. Two-dimensional arrays can also be used, like a table with rows and columns.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

INTRPRG:

Arrays

Emerico H. Aguilar

Arrays

An array is a collection of values of a single


type
You may think of it as a row of values

* from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays

Each item in the array is called an element


The position of an element in an array is denoted by
their index

The starting index of an array is 0

The last index of an array is equal to its length 1

Arrays

Arrays can be created for primitive data types


and also for object types (instance of classes)
Arrays can also be two-dimensional, with a
given number of rows and columns (like a table)

Arrays

The size of an array is defined during its creation. This


is fixed and can no longer be changed
The elements of an array can be accessed through
their index
Each element of an array should be given an initial
value before it can be accessed

Arrays
public class MyProgram {
public static void main(String args[]) {
int numberArray[] = new int[5];
numberArray[0] = 1;
numberArray[1] = 2;
numberArray[2] = 3;
numberArray[3] = 4;
numberArray[4] = 5;
System.out.println(numberArray[0]);
System.out.println(numberArray[1]);
System.out.println(numberArray[2]);
System.out.println(numberArray[3]);
System.out.println(numberArray[4]);
}
}

Arrays
public class MyProgram {
public static void main(String args[]) {
String daysOfTheWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
for (int i = 0; i < 7; i++) {
System.out.println(daysOfTheWeek[i]);
}
}
}

You might also like