In programming, an array is a collection of the homogeneous types of data stored in a consecutive memory location and each data can be accessed using its index.
In the Java programming language, we have a String data type. The string is nothing but an object representing a sequence of char values. Strings are immutable in java. Immutable means strings cannot be modified in java.
When we create an array of type String in Java, it is called String Array in Java.
To use a String array, first, we need to declare and initialize it. There is more than one way available to do so.
Declaration:
The String array can be declared in the program without size or with size. Below is the code for the same –
String[] myString0; // without size String[] myString1=new String[4]; //with size
In the above code, we have declared one String array (myString0) without the size and another one(myString1) with a size of 4. We can use both of these ways for the declaration of our String array in java.
Initialization:
//first method String[] arr0=new String[]{"Apple","Banana","Orange"}; //second method String[] arr1={"Apple","Banana","Orange"}; //third method String[] arr2=new String[3]; arr2[0]="Apple"; arr2[1]="Banana"; arr2[2]="Orange";
In the first method, we are declaring the values at the same line. A second method is a short form of the first method and in the last method first, we are creating the String array with size after that we are storing data into it.
Iteration:
To iterate through a String array we can use a looping statement.
Java
// Java program to demonstrate the various // methods to iterate over a string array public class GFG { public static void main(String[] args) { String[] arr = { "Apple" , "Banana" , "Orange" }; // First method for (String i : arr) { System.out.print(i + " " ); } System.out.println(); // Second method for ( int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + " " ); } System.out.println(); // Third method int i = 0 ; while (i < arr.length) { System.out.print(arr[i] + " " ); i++; } System.out.println(); } } |
Apple Banana Orange Apple Banana Orange Apple Banana Orange
Time Complexity: O(N), where N is length of array.
Auxiliary Space: O(1)
So generally we are having three ways to iterate over a string array. The first method is to use a for-each loop. The second method is using a simple for loop and the third method is to use a while loop. You can read more about iterating over array from Iterating over Arrays in Java
Searching:
To find an element from the String Array we can use a simple linear search algorithm. Here is the implementation for the same –
Java
// Java program to perform the searching // operation on a string array public class GFG { public static void main(String[] args) { String[] arr = { "Apple" , "Banana" , "Orange" }; String key = "Banana" ; boolean flag = false ; for ( int i = 0 ; i < arr.length; i++) { if (arr[i] == key) { System.out.println( "Available at index " + i); flag = true ; } } if (flag == false ) { System.out.println( "Not found" ); } } } |
Available at index 1
In the above code, we have a String array that contains three elements Apple, Banana & Orange. Now we are searching for the Banana. Banana is present at index location 1 and that is our output.
Sorting:
Sorting of String array means to sort the elements in ascending or descending lexicographic order.
We can use the built-in sort() method to do so and we can also write our own sorting algorithm from scratch but for the simplicity of this article, we are using the built-in method.
Java
// Java program to perform the sorting // operation on a string array import java.util.Arrays; class GFG { public static void main(String[] args) { String[] arr = { "Apple" , "Cat" , "Ball" , "Cartoon" , "Banana" , "Avocado" }; // sorting the String array Arrays.sort(arr); for (String i : arr) { System.out.print(i + " " ); } } } |
Apple Avocado Ball Banana Cartoon Cat
Here our String array is in unsorted order, so after the sort operation the array is sorted in the same fashion we used to see on a dictionary or we can say in lexicographic order.
String array to String:
To convert from String array to String, we can use a toString() method.
Java
// Java program to demonstrate the // conversion of String array to String import java.util.Arrays; class GFG { public static void main(String[] args) { String[] arr = { "The" , "quick" , "brown" , "fox" , "jumps" , "over" , "the" , "lazy" , "dog" }; // converting to string String s = Arrays.toString(arr); System.out.println(s); } } |
[The, quick, brown, fox, jumps, over, the, lazy, dog]
Here the String array is converted into a string and it is stored into a string type variable but one thing to note here is that comma(,) and brackets are also present in the string. To create a string from a string array without them, we can use the below code snippet.
Java
// Java program to demonstrate the // conversion of String array to String public class GFG { public static void main(String[] args) { String[] myarr = { "The" , "quick" , "brown" , "fox" , "jumps" , "over" , "the" , "lazy" , "dog" }; StringBuilder sb = new StringBuilder(); sb.append(myarr[ 0 ]); for ( int i = 1 ; i < myarr.length; i++) { sb.append( " " + myarr[i]); } String s = sb.toString(); System.out.println(s); } } |
The quick brown fox jumps over the lazy dog
In the above code, we are having an object of the StringBuilder class. We are appending that for every element of the string array (myarr). After that, we are storing the content of the StringBuilder object as a string using the toString() method.