In Java, an array is a collection of similar data type elements. It uses a contiguous memory location to store the elements.
A string array is an array of a fixed number of String values. A String is a sequence of characters. Generally, a string is an immutable object, which means the value of the string cannot be changed. The string array works similarly to other data types.
Consider the following points about the String Array:
We can declare string array in the following two ways:
Another way of declaring the Array is String strArray[], but the above-specified methods are more efficient and recommended.
We can initialize string array in the following ways:
Inline Initialization
Initialization after Declaration with Specific Size
In the above initialization, the value of the index can be found using the (arraylength - 1) formula. If we try to access the elements more than the index 2, it throws the Java.lang.ArrayIndexOutOfBoundsException exception.
A string array can be traversed using both the for loop and the enhanced for-each loop. These loops allow us to access and process each element of the array by iterating through the index of each element in the case of a for loop, or directly accessing each element in the case of a for-each loop.
Example
The following example demonstrates how to iterate over a string array containing names of students in a class using both a for loop and a for-each loop:
Java
Execute NowOutput:
Using for loop: Ani Sam Joe Using for-each loop: Ani Sam Joe
We can easily add the elements to the String Array just like other data types. It can be done in the following three ways:
In this method, we already have an Array of larger size. For example, if we require to store the 10 elements, then we will create an Array of size 20. It is the easiest way to expand the Array elements.
Consider the following example to add elements in a pre-allocated array.
Java
Execute NowOutput:
Original Array Elements:[A, B, C, D, E, null, null] Array after adding two elements:[A, B, C, D, E, F, G]
From the above example, we have added two elements in a pre-allocated Array.
The ArrayList is a fascinating data structure of the Java collection framework. We can easily add elements to a String Array using an ArrayList as an intermediate data structure.
Consider the following example to understand how to add elements to a String Array using ArrayList:
Java
Execute NowOutput:
Initial Array: [A, B, C, D, E, F] Array with added value: [A, B, C, D, E, F, G]
In this method, we will create a new Array with a larger size than the initial Array and accommodate the elements in it. We will copy all the elements to the newly added Array. Consider the following example:
Java
Execute NowOutput:
Initial Array: [A, B, C] updated Array: [A, B, C, D]
To search for a specific string in a string array, we use a for loop. It iterates over each element and compare each element with the target element. It both string gets matched, returns the position of the string.
Java
Execute NowOutput:
Sam String is found at index 1
Explanation
In the above example, we have initialized a boolean variable x to false and an index variable to iterate through the string. Also, we have declared a local variable String variable s to be searched. Here, the break keyword will exit the loop as soon as the string is found.
The Sort a String Array is quite easy. It is performed like sorting traditional array. We use a sort() method to sort the Array elements.
Java
Execute NowOutput:
Entered Sports: [Cricket, Basketball, Football, Badminton, Tennis] Sorted Sports: [Badminton, Basketball, Cricket, Football, Tennis]
From the above example, we can see the elements from a String Array is sorted using the sort() method.
We can also convert String Array to other data structures such as List, int Array, ArrayList, and more and vice-versa.
String arrays support various operations for manipulation, such as modifying elements. However, it's important to remember that Java arrays have a fixed size once initialized. Therefore, dynamic operations like adding or removing elements cannot be performed directly on arrays; for such operations, you may need to use other data structures like ArrayList.
The following example demonstrates how to modify an element in a string array:
Java
Execute NowOutput:
Ani Sara Joe
String arrays find application in numerous scenarios across Java programming. Some common use cases include:
We request you to subscribe our newsletter for upcoming updates.