[go: up one dir, main page]

0% found this document useful (0 votes)
40 views2 pages

Array Noets - Odt

Arrays in Java allow us to store multiple values in a single variable. An array is an object that holds a fixed number of values of a single type. Each value is accessed via an index with values ranging from 0 to the array's length-1. Arrays must be instantiated with a size that cannot change, though individual elements can. Elements are initialized to default values like 0 for primitives and null for objects. Arrays can be populated through individual assignment, initializer lists, or for loops. They can also be passed as parameters or organized into multi-dimensional structures.

Uploaded by

egg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

Array Noets - Odt

Arrays in Java allow us to store multiple values in a single variable. An array is an object that holds a fixed number of values of a single type. Each value is accessed via an index with values ranging from 0 to the array's length-1. Arrays must be instantiated with a size that cannot change, though individual elements can. Elements are initialized to default values like 0 for primitives and null for objects. Arrays can be populated through individual assignment, initializer lists, or for loops. They can also be passed as parameters or organized into multi-dimensional structures.

Uploaded by

egg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Arrays in Java – Introduction to data structures

Arrays are lists of values


Those values can be primitive data types or objects, but all the values in an array must be of the same
type.
Each value stored in an array is called an array element.
An array is an object.
An array is an object with a reference variable.
The size of the array much be declared in the instantiation and cannot be changed
The elements of an array can however be changed.
Creating an array
An int array of 10 elements can be created in any of the following ways:
1. int[] grades = new int[10];
2. int grades[] = new int[10];
3. int[] grades; *later on* grades = new int[10];
Each element must be of type integer.
The number for each position is called an index or a subscript. If an array is of length N, the index is
from 0 to N-1.
grade[5] is called “grade sub five”
grade[5] refers to the value in that position.
Initializing elements of an array
Elements in an array are initialized to:
• Zero – primitive data types (null characters for char)
• Null – objects
• False – booleans
There are three common ways to declare the elements of an array
• Each individual elements
• Initializer List
• For loops
Each individual element
int[] age = new int[4];
age[0] = 25;
age[1]…
Initializer list:
int[] age = {25,17,34,43}; ← No need for new operator this way.
For Loops (most common):
char[] letters = new char[26];
char temp=’a’;
for(int i=0; i<letters.length; i++){
letters[i]=temp;
temp++;}
Note: length is a public constant. The loop must be strictly less than the length or you will get an
ArrayIndexOutOfBoundsException.
For each loops (enhanced for loops):
Use “for each” loop shortcut to visit all elements in a collection
double[] data=…;
double sum=0;
for (double x: data){
sum=sum+x;}
for(int x=0; x<data.length();x++){
sum=sum+data[x];} //(less cool than double x: data)

String[] stringsAreKool=…;
String firstLetters;
for(String letter: stringsAreKool){
firstLetters+=letter.charAt(0);}

Arrays as parameters
Be careful when passing an element of an array as a parameter.

Array of arrays
int[] grades = new int[10];
grades[] classGrades = new grades[25];
This is could be a 2D array:
int[] [] grades = new int[10][25];
The first dimension is rows, the second is columns.

You might also like