Quantcast
Channel: Oracle Fusion Apps | Oracle Fusion | Oracle Apps Training
Viewing all articles
Browse latest Browse all 103

Arrays and Strings in JAVA

$
0
0

Objective:

In the previous article looping constructs and conditional statements in Java, we have seen different types of loops and conditional statement that are used in java. In this article we will learn Arrays and strings in Java.

 

Arrays:

An array is the data structure which hold the sequential value of the same type.You can create an array by using the new operator with the following syntax:

arrayRefVar = new dataType[arraySize]

The way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for 10 integer elements and assigns the array to the anArray variable.

// create an array of integers

anArray = new int[10];

If this statement is missing, then the compiler prints an error like the following, and compilation fails:

ArrayDemo.java:4: Variable anArray may not have been initialized.

 

The next few lines assign values to each element of the array:

anArray[0] = 100; // initialize first element

anArray[1] = 200; // initialize second element

anArray[2] = 300; // and so forth

Each array element is accessed by its numerical index:

 

System.out.println("Element 1 at index 0: " + anArray[0]);

System.out.println("Element 2 at index 1: " + anArray[1]);

System.out.println("Element 3 at index 2: " + anArray[2]);

 

Alternatively, you can use the shortcut syntax to create and initialize an array:

int[ ] anArray = {

   100, 200, 300,

   400, 500, 600,

   700, 800, 900, 1000

};

Here the length of the array is determined by the number of values provided between braces and separated by commas.

Example 1:

c1.png

for(int i=0;i<a.length;i++)

The above for loop is known as the declaration of the array.


Viewing all articles
Browse latest Browse all 103

Trending Articles