String Array in Java

An Array is an essential and most used data structure in Java. It is one of the most used data structure by programmers due to its efficient and productive nature; The 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 can not be changed. The String Array works similarly to other data types of Array.

In Array, only a fixed set of elements can be stored. It is an index-based data structure, which starts from the 0th position. The first element will take place in Index 0, and the 2nd element will take place in Index 1, and so on.

The main method {Public static void main[ String [] args]; } in Java is also an String Array.

Consider the below points about the String Array:

  • It is an object of the Array.
  • It can be declared by the two methods; by specifying the size or without specifying the size.
  • It can be initialized either at the time of declaration or by populating the values after the declaration.
  • The elements can be added to a String Array after declaring it.
  • The String Array can be iterated using the for loop.
  • The searching and sorting operation can be performed on the String Array.

Declaration:

The Array declaration is of two types, either we can specify the size of the Array or without specifying the size of the Array. A String Array can be declared as follows:

Another way of declaring the Array is String strArray[], but the above-specified methods are more efficient and recommended.

Initialization:

The String Array can be initialized easily. Below is the initialization of the String Array:

All of the above three ways are used to initialize the String Array and have the same value.

The 3rd method is a specific size method. In this, the value of the index can be found using the ( arraylength - 1) formula if we want to access the elements more than the index 2 in the above Array. It will throw the Java.lang.ArrayIndexOutOfBoundsException exception.

Let's see an example of String Array to demonstrate it's behavior:

Iteration of String Array

The String Array can be iterated using the for and foreach loop. Consider the below code:

Adding Elements to a String Array

We can easily add the elements to the String Array just like other data types. It can be done using the following three methods:

  • Using Pre-Allocation of the Array
  • Using the Array List
  • By creating a new Array

let's understand the above methods:

Using Pre-Allocation of the Array:

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 below example to add elements in a pre-allocated array.

Output:

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.

Using ArrayList:

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 below example to understand how to add elements to a String Array using ArrayList:

Output:

Initial Array:
[A, B, C, D, E, F]
Array with added value: 
[A, B, C, D, E, F, G]

By Creating a New Array:

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 below example:

Output:

Initial Array: [A, B, C]
updated Array: [A, B, C, D]

This is how we can add elements to a String Array. Let's understand how to search and sort elements in String Array.

Searching in String Array

For searching a String from the String Array, for loop is used. Consider the below example:

Output:

Sam String is found at index 1

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.

Sorting in String Array

The sorting in the String array is quite easy. It is performed like in a traditional array. We use a sort() method to sort the Array elements. Sorting is easier than searching.

Consider the below example to sort a String Array:

Output:

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.






Latest Courses