String Arrays in Java
A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.
When we create an array of type String in Java, it is called a String Array in Java. In this article, we will learn the concepts of String Arrays in Java including declaration, initialization, iteration, searching, sorting, and converting a String Array to a single string.
Table of Content
To use a String array, first, we need to declare and initialize it. There are more than one way available to do so.
Declaration of String Arrays
A String array can be declared with or without specifying its size. Below is the example:
String[] str0; // declaration without size String[] str1 = new String[4]; //declaration with size
In the above example,
- str0 is declared without specifying its size.
str1
is declared with a size of 4.
We can use both of these ways to declare String array in Java.
Initialization of String Arrays
Declaring and Initializing Together
In this method, we are declaring the values at the same line.
String[] arr0 = new String[]{"Apple", "Banana", "Orange"};
Using Short Form
This method is the short form of the first method i.e. initialize the array at the time of declaration.
String[] arr1={"Apple","Banana","Orange"};
Declaring First, Initializing Later
In this method, we are declaring the String array with size first and after that we are storing data into it.
String[] arr2=new String[3];
arr2[0]="Apple";
arr2[1]="Banana";
arr2[2]="Orange";
Iterating Over String Arrays
To iterate through a String array we can use a looping statement. So generally we have 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.
Example:
// Java program to demonstrate 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();
}
}
Output
Apple Banana Orange Apple Banana Orange Apple Banana Orange
Searching in a String Array
To find an element from the String Array we can use a simple linear search algorithm.
Example:
// Java program to perform the searching
// operation on a string array
public class GFG {
public static void main(String[] args) {
// Initialize a String array with some elements
String[] arr = { "Apple", "Banana", "Orange" };
// Key to search in the array
String k = "Banana";
// Flag to track if the key is found
boolean f = false;
// Iterate over the array to search for the key
for (int i = 0; i < arr.length; i++) {
// Check if the current element matches the key
if (arr[i] == k) {
// If found, print the index and set the flag to true
System.out.println("Available at index " + i);
f = true;
}
}
// If the key is not found, print "Not found"
if (f == false) {
System.out.println("Not found");
}
}
}
Output
Available at index 1
Sorting a String Array
Sorting a String array means to sort the elements in lexicographical (dictionary) order. We can use the built-in sort() method to do so and we can also write our own sorting algorithm from scratch.
// 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 + " ");
}
}
}
Output
Apple Avocado Ball Banana Cartoon Cat
Converting String Array to String
Using Arrays.toString()
To convert from String array to String, we can use a Arrays.toString() method or a custom approach.
Example:
// 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
= { "She", "is", "a", "good", "girl" };
// converting to string
String s = Arrays.toString(arr);
System.out.println(s);
}
}
Output
[She, is, a, good, girl]
Explanation: Here, the String array is converted into a string, but one thing to note here is that comma(,) and brackets are also present in the string.
Custom Conversion Without Brackets and Commas
To create a string from a string array without comma(,) and brackets , we can use the below code snippet.
// Java program to demonstrate the
// conversion of String array to String
public class GFG {
public static void main(String[] args) {
// Initialize a String array with some elements
String[] arr = { "She", "is", "a", "good", "girl" };
// Create a StringBuilder to build the resulting string
StringBuilder sb = new StringBuilder();
// Append the first element of the array to the StringBuilder
sb.append(arr[0]);
// Loop through the remaining elements in the array
for (int i = 1; i < arr.length; i++) {
// Append each element with a space in between
sb.append(" " + arr[i]);
}
// Convert the StringBuilder content into a String
String s = sb.toString();
System.out.println(s);
}
}
Output
She is a good girl