String Array in Java
String Array in Java
An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types like primitive or non-primitive.
What is a String array in Java?
In Java,a String array is a fixed size objectthat stores string values. The String is a sequence of characters. It is an immutable object it means that values stored in the array object cannot be modified.
Declaring a String array in Java
There are the following ways to declare a String array in Java.
- Declaring without size
Syntax:
String[] myStrArray;
This way a String array myStrArraycan be declared like any other normal variable. But before using this, it should be instantiated with new.
- Declaring with size
Syntax:
String[] myStrArray = new String[5];
Here, the String array myStrArray is declared using the newkeyword. It can store five elements.
Initializing a String array
There are two ways to initialize an array:
Inline Initialization: In inline initialization, first we declare an array after that put the elements of the array.
Syntax:
String[] myStrArray1 = new String[]{"a", "b", "c"}; String[] myStrArray2 ={"a", "b", "c"};
Here, myStrArray1 is declared and initialized immediately. And myStrArray2 is instantiated just after declaration.
We can also split the first statement, as follows:
String[] myStrArray1; // array declaration myStrArray1 = new String[] { "A", "B", "C", "D", "E" }; // array initialization
Initialization after declaration
Syntax:
String[] strarr = new String[4]; strarr[0] = "a"; strarr[1] = "b"; strarr[2] = "c"; strarr[3] = "d";
Here, the strarr is first declared and then it is initialized one by one using the index.
Operations Performed on String array
- Size of String array
The Java String class provides a property called length. It determines the length of the string.
StringSize.java
public class StringSize { /* Driver Code */ public static void main(String []args) { /* String Array declaration and initialization */ String[] myStrArr1 ={"a", "b", "c"}; /*prints length of array */ System.out.println("Length of the String array: "+myStrArr1.length); } }
Output:
Length of the String array: 3
In the above class StringSize, a String array myStrArr1 is declared and initialized immediately. Length property is used to print the length of myStrArr1.
- Iterating in an array
IterateString.java
public class IterateString { /* Driver Code */ public static void main(String []args) { /* String Array declaration and initialization */ String[] myStrArr1 ={"a", "b", "c"}; /*iterates over array */ for(int i=0;i<myStrArr1.length;i++) System.out.println("myStrArr1["+i+"]: "+myStrArr1[i]); } }
Output:
myStrArr1[0]: a myStrArr1[1]: b myStrArr1[2]: c
In the above program we have used Java for loop that iterates over the array. The loop starts from the 0 and execute till the length of the array.
- Searching an element in a String array
SearchStrArray.java
public class SearchStrArray { /*Driver Code */ public static void main(String[] args) { /* String Array declaration and initialization */ String[] myStrArr1 = { "a", "b", "c" }; boolean flag = false; int index = 0; String k = "b"; /* Element to be searched */ for (int i = 0; i < myStrArr1.length; i++) { if(k.equals(myStrArr1[i])) { index = i; flag = true; break; /*stops the loop after element is found */ } } if(flag) System.out.println("Element "+ k +" found at the index "+ index); else System.out.println("Element "+ k +" not found in the array"); } }
Output:
Element b found at the index 1
In the above program, we have searched for the element b. In the if statement we have compared the element (b) with each element of the array. If match is not found, the break keyword breaks the execution of the loop and the execution jumps to the next statement.
- Sorting a String array
SortStrArray.java
import java.util.*; public class SortStrArray { /*Driver Code */ public static void main(String[] args) { /* String Array declaration and initialization */ String[] myStrArr = { "c", "a", "b", "f", "e" }; System.out.println("Before Sorting: "+ Arrays.toString(myStrArr)); /*Sorting operation */ Arrays.sort(myStrArr); System.out.println("After Sorting: "+ Arrays.toString(myStrArr)); } }
Output:

In the above code, Arrays.sort(myStrArr) method is used to sort the String array.
- Converting a String array to a String
StrArraytoStr.java
import java.util.*; public class StrArraytoStr { /*Driver Code */ public static void main(String[] args) { /* String Array declaration and initialization */ String[] myStrArr = { "c", "a", "b", "f", "e" }; /* String array to String conversion */ String theString = Arrays.toString( myStrArr); System.out.println("String: "+ theString); } }
Output:

In the above code, the String array is converted into String using Arrays.toString(myStrArr) method.
- Converting a String array to a List and adding a new element to the List
StrArraytoList.java
import java.util.*; public class StrArraytoList { /*Driver Code */ public static void main(String[] args) { /* String Array declaration and initialization */ String[] myStrArr = { "a", "b", "c", "e", "f" }; /* String array to List conversion */ List<String> fixedList = Arrays.asList(myStrArr); System.out.print("String array to List: "); for (String str : fixedList) { System.out.print(str + " " ); } /* A new list for adding new elements */ List<String> stringList = new ArrayList<String>( fixedList ); /*Adding a new element to the List */ stringList.add( "g" ); System.out.println(); System.out.print("List after adding a new element: "); for (String str : stringList) { System.out.print(str + " " ); } } }
Output:

In the above code, Arrays.asList(myStrArr) method is used to convert the String array myStrArr to a List.
In this way, we have understood String arrays in Java and various operations performed on them.