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

Java Arrays PDF

1) Java arrays allow storing and accessing multiple values of the same type. Arrays are declared with a type followed by square brackets, like int[] or String[]. 2) Array values can be initialized during declaration by providing values within curly braces, such as int[] numbers = {1, 2, 3}. 3) Individual array elements are accessed using their numeric index within square brackets, like arrayName[0] to get the first element.

Uploaded by

Johnmark Padilla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
251 views

Java Arrays PDF

1) Java arrays allow storing and accessing multiple values of the same type. Arrays are declared with a type followed by square brackets, like int[] or String[]. 2) Array values can be initialized during declaration by providing values within curly braces, such as int[] numbers = {1, 2, 3}. 3) Individual array elements are accessed using their numeric index within square brackets, like arrayName[0] to get the first element.

Uploaded by

Johnmark Padilla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Here, age array can hold 5 values of type int.

Java Arrays It's possible to declare and allocate memory of an array in one
statement. You can replace two statements above with a single
An array is a container that holds data (values) of one single type. For statement.
example, you can create an array that can hold 100 values
of int type.Array is a fundamental construct in Java that allows you to int[] age = new int[5];
store and access large number of values conveniently.

How to declare an array?


Here's how you can declare an array in Java: Java Array Index
You can access elements of an array by using indices. Let's consider
dataType[] arrayName; previous example.

int[] age = new int[5];


• dataType can be a primitive data type like: int, char, Double, byte etc.
or an object (will be discussed in later chapters).
• arrayName is an identifier.

double[] data;

Here, data is an array that can hold values of type Double.


The first element of array is age[0], second is age[1] and so on.
But, how many elements can array this hold?
If the length of an array is n, the last element will be arrayName[n-1].
Good question! We haven't defined it yet. The next step is to allocate
memory for array elements. Since the length of age array is 5, the last element of the array
is age[4] in the above example.
The default initial value of elements of an array is 0 for numeric types
data = new Double[10]; and false for boolean. We can demonstrate this:
1. class ArrayExample {
2. public static void main(String[] args) {
3.
The length of data array is 10. Meaning, it can hold 10 elements 4. int[] age = new int[5];
(10 Double values in this case).Note, once the length of the array is 5.
defined, it cannot be changed in the program. 6. System.out.println(age[0]);
Let's take another example: 7. System.out.println(age[1]);
8. System.out.println(age[2]);
int[] age; 9. System.out.println(age[3]);
10. System.out.println(age[4]);
11. }
age = new int[5];
12. }
When you run the program, the output will be:

0
0
0 Let's write a simple program to print elements of this array.
0
0
1. class ArrayExample {
There is a better way to access elements of an array by using looping 2. public static void main(String[] args) {
construct (generally for loop is used). 3.
1. class ArrayExample { 4. int[] age = {12, 4, 5, 2, 5};
2. public static void main(String[] args) { 5.
3. 6. for (int i = 0; i < 5; ++i) {
4. int[] age = new int[5]; 7. System.out.println("Element at index " + i
5. +": " + age[i]);
6. for (int i = 0; i < 5; ++i) { 8. }
7. System.out.println(age[i]); 9. }
8. } 10. }
9. }
10. } When you run the program, the output will be:

How to initialize arrays in Java?


In Java, you can initialize arrays during declaration or you can Element at index 0: 12
initialize (or change values) later in the program as per your Element at index 1: 4
requirement. Element at index 2: 5
Element at index 3: 2
Initialize an Array During Declaration Element at index 4: 5
Here's how you can initialize an array during declaration.
How to access array elements?

int[] age = {12, 4, 5, 2, 5}; You can easily access and alter array elements by using its numeric
index. Let's take an example.

This statement creates an array and initializes it during 1. class ArrayExample {


declaration.The length of the array is determined by the number of 2. public static void main(String[] args) {
values provided which is separated by commas. In our example, the 3.
length of age array is 5. 4. int[] age = new int[5];
5.
6. // insert 14 to third element
7. age[2] = 14;
8. 15. average = ((double)sum /
9. // insert 34 to first element (double)arrayLength);
10. age[0] = 34; 16.
11. 17. System.out.println("Sum = " + sum);
12. for (int i = 0; i < 5; ++i) { 18. System.out.println("Average = " + average);
13. System.out.println("Element at index " + 19. }
i +": " + age[i]); 20. }
14. }
15. } When you run the program, the output will be:
16. }

When you run the program, the output will be: Sum = 36
Average = 3.6

Element at index 0: 34
Element at index 1: 0 Couple of things here.
Element at index 2: 14 • The for..each loop is used to access each elements of the array.
Element at index 3: 0
Learn more on how for...each loop works in Java.
Element at index 4: 0
• To calculate the average, int values sum and arrayLength are
converted into double since average is double. This is type casting.
Learn more on Java Type casting.
Example: Java arrays
• To get length of an array, length attribute is used.
The program below computes sum and average of values stored in an
Here, numbers.length returns the length of numbers array.
array of type int.
1. class SumAverage {
Multidimensional Arrays
2. public static void main(String[] args) {
Arrays we have mentioned till now are called one-dimensional arrays.
3.
In Java, you can declare an array of arrays known as
4. int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9,
multidimensional array. Here's an example to declare and initialize
8, 12};
multidimensional array.
5. int sum = 0;
6. Double average; Double[][] matrix = {{1.2, 4.3, 4.0},
7.
8. for (int number: numbers) {
9. sum += number; {4.1, -1.1}
10. }
11.
12. int arrayLength = numbers.length; };
13.
14. // Change sum and arrayLength to double as
average is in double
{4, 5, 6, 9},

Java Multidimensional Arrays {7},


Here, a is a two-dimensional (2d) array. The array can hold
maximum of 12 elements of type int. };

As mentioned, each component of array a is an array in itself,


and length of each rows is also different.

Remember, Java uses zero-based indexing, that is, indexing of


arrays in Java starts with 0 and not 1.
Similarly, you can declare a three-dimensional (3d) array. For
example,
Let's write a program to prove it.
String[][][] personalInfo = new String[3][4][2];
1. class MultidimensionalArray {
2. public static void main(String[] args) {
3.
Here, personalInfo is a 3d array that can hold maximum of 24 4. int[][] a = {
(3*4*2) elements of type String. 5. {1, 2, 3},
6. {4, 5, 6, 9},
How to initialize a 2d array in Java? 7. {7},
8. };
9.
Here's an example to initialize a 2d array in Java. 10. System.out.println("Length of row 1: " +
a[0].length);
11. System.out.println("Length of row 2: " +
int[][] a = { a[1].length);
12. System.out.println("Length of row 3: " +
a[2].length);
{1, 2, 3}, 13. }
14. }
When you run the program, the output will be: 12. System.out.println(data);
13. }
14. }
15. }
Length of row 1: 3
16. }
Length of row 2: 4
Length of row 3: 1
When you run the program, the output will be:
Since each component of a multidimensional array is also an
array (a[0], a[1] and a[2] are also arrays), you can
use length attribute to find the length of each rows. 1

Example: Print all elements of 2d array Using Loop


1. class MultidimensionalArray { -2
2. public static void main(String[] args) {
3.
3
4. int[][] a = {
5. {1, -2, 3},
6. {-4, -5, 6, 9}, -4
7. {7},
8. };
9. -5
10. for (int i = 0; i < a.length; ++i) {
11. for(int j = 0; j < a[i].length; ++j) {
12. System.out.println(a[i][j]); 6
13. }
14. }
15. } 9
16. }
It's better to use for..each loop to iterate through arrays 7
whenever possible. You can perform the same task
using for..each loop as:
1. class MultidimensionalArray {
2. public static void main(String[] args) { How to initialize a 3d array in Java?
3.
4. int[][] a = {
You can initialize 3d array in similar way like a 2d array. Here's
5. {1, -2, 3},
6. {-4, -5, 6, 9}, an example:
7. {7},
8. };
9. // test is a 3d array
10. for (int[] innerArray: a) {
11. for(int data: innerArray) {
int[][][] test = { 8. {2, 3, 4}
9. },
10. {
{ 11. {-4, -5, 6, 9},
12. {1},
13. {2, 3}
{1, -2, 3},
14. }
15. };
{2, 3, 4} 16.
17. // for..each loop to iterate through elements of 3d
array
}, 18. for (int[][] array2D: test) {
19. for (int[] array1D: array2D) {
20. for(int item: array1D) {
{ 21. System.out.println(item);
22. }
23. }
{-4, -5, 6, 9}, 24. }
25. }
{1}, 26. }

When you run the program, the output will be:


{2, 3}

} 1
-2
3
}; 2
3
4
-4
Basically, 3d array is an array of 2d arrays.Similar like 2d arrays, -5
rows of 3d arrays can vary in length. 6
9
1
Example: Program to print elements of 3d array using loop 2
1. class ThreeArray { 3
2. public static void main(String[] args) {
3.
4. // test is a 3d array
5. int[][][] test = {
6. {
7. {1, -2, 3},

You might also like