You are on page 1of 3

JAVA ARRAY

An array is a fundamental and crucial data structure Java programming language. It is


highly used by programmers due to its efficient and productive nature. A Java String
Array is an object that holds a fixed number of String values. In this tutorial, let us dig a bit
deeper and understand the concept of String array in Java.
What is a String Array in Java

You must be aware of Java Arrays, it is an object that contains elements of a similar data
type. Also, they are stored in a continuous memory location. Strings, on the other hand, is a
sequence of character. It is considered as immutable object i.e, the value cannot be
changed. java String array works in the same manner. String Array is used to store a fixed
number of Strings.

Now, let’s have a look at the implementation of Java string array.

Declaring A String Array In Java

For implementation ensure you get Java Installed. A string array is declared by the
following methods:

1 String[] stringArray1 //declaring without size


2 String[] stringArray2 = new String[2]; //declaring with size
The string array can also be declared as String strArray[], but the previously mentioned
methods are favoured and recommended. Note that the value of strArray1 is null, while the
value of strArray2 is [null, null].

Now let us move ahead and checkout how to initialize a string array,

Initializing A String Array In Java

A string array can be initialized by using:

1 //inline initialization
2 String[] stringArray1 = new String[] {"R","S","T"};
3 String[] stringArray2 = {"R","S","T"};
4 //initialization after declaration
5 String[] stringArray3 = new String[3];
6 stringArray3[0] = "R";
7 stringArray3[1] = "S";
8 stringArray3[2] = "T";
All three arrays specified above have the same values.

Since there are only 3 elements in stringArray3, and the index starts from 0, the last index is
3. We could also use the formula (array length – 1) to ascertain the value of the index. On
accessing an index greater than 2, an exception will be raised.
Example:

1 String[] stringArray3 = new String[3];


2 stringArray3[3] = "U";
 This will throw a java.lang.ArrayIndexOutOfBoundsException.

Initialization of a string array can be done along with the declaration by using the new
operator in the syntax:

1 <strong>String</strong>[] stringArray3 = new <strong>String</strong>[]{“R”,”S”,”T”};


Let us continue with the next topic of this article,

Size Of A String Array

The property length  of a string array can be used to determine the number of elements in
the Array.

1 <strong>String</strong>[] stringArray3 = {“R”,”S”,”T”};


System.out.println( stringArray3.length);

Output:

array

Iterating In A String Array

Iteration over a string array is done by using java for loop, or java for each loop.

1 String[] strArray3 = {“R”,”S”,”T”};


2 //iterating all elements in the array
3 for (int i = 0; i < strArray3.length; i++) {
4 System.out.print(strArray3[i]);
5 }
The code starts from index 0, and continues up to length – 1, which is the last element of
the array.

Output:

We can also make use of the enhanced for loop provided by Java 5:

1 //iteration by using the enhanced for loop provided by Java 5 or later


2 for (String str : strArray3) {
3 System.out.print(str);
4 }
Let us move further with this article on String Array In Java,
Searching Through A String Array

In case the user wants to search for a specific value in the string array, for loop is used.

public class SearchStringArrayExample {


public static void main(String[] args) {
1
String[] strArray3 = { "R", "S", "T" };
2
boolean found = false;
3
int index = 0;
4
String s = "S";
5
for (int i = 0; i < strArray.length; i++) {
6
if(s.equals(strArray[i])) {
7
index = i; found = true; break;
8
}
9
}
10
if(found)
11
System.out.println(s +" found at the index "+index);
12
else
System.out.println(s +" not found in the array");}}
Output:

B found at index 1

The keyword break is used to stop the loop as soon as the element is found.

Sorting A String Array

To sort the elements in the string array, we can implement our own sorting algorithm, or
we can invoke the Arrays class sorting method.

1 String[] vowels = {"o","e","i","u","a"};


2 System.out.println("Initial array "+Arrays.toString(vowels));
3 Arrays.sort(vowels);
4 System.out.println("Array after the sort "+Arrays.toString(vowels));
Output:

Initial array: [o ,e , i, u, a]

Array after the sort: [a, e, i, o, u]

You might also like