Arrays


An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.


An array of 10 elements.


Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.




  1. Create an Array in Java
  2. Create Different Array in Different ways in Java
  3. Array of Objects in Java
  4. Create Two Dimensional Array in Java
  5. Create Three Dimensional Array in Java

A. Create an Array in Java

Array_Ex1.java

public class Array_Ex1 {
public static void main(String args[]) {
int[] intArray = new int[6];
intArray[0] = 100;
intArray[1] = 200;
intArray[2] = 300;
intArray[3] = 400;
intArray[4] = 500;
intArray[5] = 600;
for(int i=0; i
System.out.println(i+1 + ". value is : " + intArray[i]);
}
}
}

Sample Output

1. value is : 100
2. value is : 200
3. value is : 300
4. value is : 400
5. value is : 500
6. value is : 600




B. Create Different Array in Different ways in Java

Array_Ex2.java

public class Array_Ex2 {

public static void main(String args[]) {
int[] intArray = new int[3];
float[] floatArray = {(float) 12.34, (float) 56.32};
double[] doubleArray = {12.32456789, 45.65789123};
boolean[] booleanArray = null;
char[] charArray1 = {'B', 'a', 'l', 'a'};
char[] charArray2 = {'J', 'a', 'v', 'a'};
String strArray[] = new String[2];
for(int i=0; i
intArray[i] = (i+1) * 10;
}
booleanArray = new boolean[2];
booleanArray[0] = true;
booleanArray[1] = false;
strArray[0] = new String(charArray1);
strArray[1] = new String(charArray2);
System.out.println("The Different Array Values");
System.out.println("\nInt Array Values");
for(int i=0; i
System.out.print(intArray[i] + " \t");
}
System.out.println("\n\nFloat Array Values");
for(int i=0; i
System.out.print(floatArray[i] + " \t");
}
System.out.println("\n\nDouble Array Values");
for(int i=0; i
System.out.print(doubleArray[i] + " \t");
}
System.out.println("\n\nBoolean Array Values");
for(int i=0; i
System.out.print(booleanArray[i] + " \t");
}
System.out.println("\n\nString Array Values");
for(int i=0; i
System.out.print(strArray[i] + " \t");
}
}
}

Sample Output

The Different Array Values

Int Array Values
10 20 30 

Float Array Values
12.34 56.32 

Double Array Values
12.32456789 45.65789123 

Boolean Array Values
true false 

String Array Values
Bala Java 




C. Array of Objects in Java

Array_Ex3.java

public class Array_Ex3 {
int a, b;
void setData(int x, int y) {
a = x;
b = y;
}
void showData() {
System.out.println("Value of a is : " + a);
System.out.println("Value of b is : " + b);
}
}

class MainClass {
public static void main(String args[]) {
Array_Ex3 obj[] = new Array_Ex3[2];
obj[0] = new Array_Ex3();
obj[1] = new Array_Ex3();
obj[0].setData(10, 20);
obj[1].setData(100, 200);
System.out.println("The First Array Object's value");
obj[0].showData();
System.out.println("\nThe Second Array Object's value");
obj[1].showData();
}
}

Sample Output

The First Array Object's value
Value of a is : 10
Value of b is : 20

The Second Array Object's value
Value of a is : 100
Value of b is : 200




D. Create Two Dimensional Array in Java

Array_Ex4.java

import java.util.Scanner;

public class Array_Ex4 {

int multi[][] = new int[2][2];
Scanner scan = new Scanner(System.in);
void insert() {
System.out.println("Insert Data into Two-Dimensional Array");
for(int i=0; i<2 i="" span="">
for(int j=0; j<2 j="" span="">
multi[i][j] = scan.nextInt();
}
}
}
void display() {
System.out.println("\nThe Two-Dimensional Array");
for(int i=0; i<2 i="" span="">
for(int j=0; j<2 j="" span="">
System.out.print(multi[i][j] + "\t");
}
System.out.println();
}
}
}

class MainClass {
public static void main(String args[]) {
Array_Ex4 obj = new Array_Ex4();
obj.insert();
obj.display();
}
}

Sample Output

Insert Data into Two-Dimensional Array
1
2
3
4

The Two-Dimensional Array
1 2
3 4




E. Create Three Dimensional Array in Java

Array_Ex5.java

import java.util.Scanner;

public class Array_Ex5 {

int multi[][][] = new int[2][2][2];
Scanner scan = new Scanner(System.in);
void insert() {
System.out.println("Insert Data into Three-Dimensional Array");
for(int i=0; i<2 i="" span="">
for(int j=0; j<2 j="" span="">
for(int k=0; k<2 k="" span="">
multi[i][j][k] = scan.nextInt();
}
}
}
}
void display() {
System.out.println("\nThe Three-Dimensional Array");
for(int i=0; i<2 i="" span="">
for(int j=0; j<2 j="" span="">
for(int k=0; k<2 k="" span="">
System.out.print(multi[i][j][k] + " ");
}
System.out.print("\t");
}
System.out.println();
}
}
}

class MainClass {
public static void main(String args[]) {
Array_Ex5 obj = new Array_Ex5();
obj.insert();
obj.display();
}
}

Sample Output

Insert Data into Three-Dimensional Array
1
2
3
4
5
6
7
8

The Three-Dimensional Array
1 2 3 4 
5 6 7 8 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.