String Arrays in JavaLast Updated : 6 Jan 2026 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. What is a String Array?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. Similar to arrays, in string 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:
Declaration of String ArrayThe 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 of String ArrayThe 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. Iteration (Traversing) of String ArrayA string array can be traversed using both the for loop and the enhanced for-each loop. These loops allow you 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. ExampleThe following example demonstrates how to iterate through a string array containing names of students in a class using both a for loop and a for-each loop: Output: Using for loop: Ani Sam Joe Using for-each loop: Ani Sam Joe Adding Elements to a String ArrayWe can easily add the elements to the String Array just like other data types. It can be done using the following three methods:
let's understand the above methods: 1. Using Pre-Allocation of the ArrayIn 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. 2. Using ArrayListThe 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] 3. By Creating a New ArrayIn 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: 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 ArrayTo search for a specific string in a string array, we can use a for loop to iterate through each element and check if it matches the target string using the equals() method that returns the position of the string or verify if it exists in the array. ExampleThe following example demonstrates how to search for a name in a string array: 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 ArrayThe 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. ExampleThis example demonstrate how to sort string array in Java. 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. Manipulating String ArraysString 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. Example: Modifying Elements in a String ArrayThe following example demonstrates how to modify an element in a string array: Output: Ani Sara Joe Common Use CasesString arrays find application in numerous scenarios across Java programming. Some common use cases include:
Next TopicHow-to-create-array-in-java |
We request you to subscribe our newsletter for upcoming updates.