Sie sind auf Seite 1von 16

Arrays

1
Arrays - Introduction
 An array is a group of contiguous
or related data items that share a 0 70
common name.
1 56
 Each value is stored at a specific index
position. Position is called a index 2 89
or superscript. Base index = 0
3 12 values
 The ability to use a single name to
4 23
represent a collection of items and
refer to an item by specifying the 5 10
item number enables us to develop
concise and efficient programs. 6 12

2
Declaration of Arrays
 Like any other variables, arrays must declared and created before they can
be used.
Creation of arrays involve three steps:
◦ Declare the array
◦ Create storage area in primary memory.
◦ Put values into the array (i.e., Memory location)
Declaration of Arrays:
◦ Form 1:
◦ Data-type arrayname[]
◦ Form 2:
◦ Data-type [] arrayname;
◦ Examples: int[ ] marks; int marks[ ];
Note: we don’t specify the size of arrays in the declaration.

3
Creation of Arrays
After declaring arrays, we need to allocate memory for storage array
items. In Java, this is carried out by using “new” operator, as follows:
Arrayname = new type[size];
Example:
marks = new int[10];

Declaration and storage in single statement:


int[] marks=new int[10];

4
Setting values of Array elements
Once arrays are created, they need to be set with some values before their
content is used.
Arrayname [index/subscript] = value;
Example:
marks[0] = 50;
marks[1] = 90;
Java creates arrays starting with subscript 0 and ends with value one less
than the size specified.
Trying to access an array beyond its boundaries will generate an error
message.

5
Arrays – Initializing
Arrays can be initialised like standard variables at the time of their
declaration.
Type arrayname[] = {list of values};
Example:
int[] marks = {55, 69, 70, 30, 80};
Creates and initializes the array of integers of length 5.
In this case it is not necessary to use the new operator.

6
Default Initialization
When array is created, array elements are initialized
◦ Numeric values (int, double, etc.) to 0
◦ Boolean values to false
◦ Char values to ‘\u0000’ (unicode for blank character)
◦ Class types to null
Arrays – Length
Arrays are fixed length . Length is specified at the time of creation:
In java, all arrays store the allocated size in a variable named “length”.
We can access the length of arrays as arrayName.length:
e.g. int x =marks.length; // x = 10

8
What happens if …
Valid code:
int k=7;
int[] marks = new int[k];

Invalid Code:
int marks[7]; // The C++ style is not permitted in JAVA syntax
OR
int k;
long[] primes =new long[k]; // variable k might not have been initialized
Reusing Array Variables
Like a variable can refer to different values at different points in the
program we can use array variables to access different arrays:
int[] marks=new int[10];
……
marks=new int[50];
Previous array will be discarded
Cannot alter the type of array
Multi-Dimensional Arrays
Two-Dimensional arrays:
COLUMN# COLUMN# COLUMN#
Example: 1 2 3

float[][] students=new float[4][3];


4 arrays each having 3 elements ROW#1

First index: specifies row


Second Index: specifies element in ROW#2

column

ROW #3

ROW#4
Initializing 2-D Arrays
int[][] array2D = { {99, 42, 74, 83, 100}, {90, 91, 72, 88, 95}, {88, 61, 74, 89,
96}, {61, 89, 82, 98, 93}, {93, 73, 75, 78, 99}, {50, 65, 92, 87, 94}, {43, 98,
78, 56, 99} };
//7 rows with 5 columns
Multi-dimensional Arrays of Varying
Length
Java treats multidimensional arrays as “arrays of arrays”. It is possible to
declare a 2D arrays as follows:
int a[][] = new int [3][];
a[0]= new int [3];
a[1]= new int [2];
a[2]= new int [4];

Note: Not required to define all arrays


Initializing Varying Size Arrays
int[][] unevenarray = { { 10, 19, 4 }, { 30, 24}, { 10, 11, 2, 3, 4 } };
//Three arrays
//First array has 3 elements
//Second array has 2 elements
//Third array has 5 elements
Array of Arrays Length
long[][] marks = new long[20][];
marks[2] = new long[30];
System.out.println(marks.length); //Number of arrays
System.out.println(marks[2].length);//Number of elements in the second
array

OUTPUT:
20
30
Enhanced for – for each
The general form of the for-each version of the for is shown here:
for(type itr-var : collection) statement-block
 Array is an example of a collection

int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
int sum = 0;
for(int i=0; i < 10; i++) sum += nums[i];

int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
int sum = 0;
for(int x: nums) sum += x;

16

Das könnte Ihnen auch gefallen