What are Array strings in Java?
In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their index.
A String data type exists in the Java computer language. The string is simply an object that represents a series of char values. In Java, strings are immutable. Strings in Java are immutable, which means they can't be changed.String Array is the name given to an array of type String created in Java.
We must declare and initialize a String array before we can utilize it. There are several options available to accomplish this.
Declaration:
In the program, the String array can be specified with or without size. The code for this is as follows:
Without size→String[] myString0;
With size→String[] myString1=new String[7];
According to the above code, we declared two String arrays one without a size (myString0) and one with a size of 7 (myString1). Both of these methods can be used to declare a String array in Java.
Initializing:
first method
String[] arr0=new String[]{"Table","Chair","Bed"};
Second method
String[] arr1={"Table","Chair","Bed"};
Third method
String[] arr2=new String[3];
arr2[0]="Table";
arr2[1]="Chair";
arr2[2]="Bed";
The values are declared on the same line in the first method. The second approach is a condensed version of the first, and the last method involves first constructing a String array of size and then storing data in it.
Iteration:
public class GFG {
public static void main(String[] args)
{
String[] arr = { "Table", "Chair", "Bed" };
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:
Table Chair Bed
Table Chair Bed
Table Chair Bed
So there are 3 methods to iterate over a string array in general. A for-each loop is the first approach. The second technique employs a basic for loop, whereas the third technique employs a while loop. Iterating over Arrays in Java has more information on iterating over arrays.
Searching for an Element in an Array:
We can use a basic linear search technique to find an entry in the String Array. The implementation for the same may be found here.
public class GFG {
public static void main(String[] args)
{
String[] arr = { "Table", "Chair", "Bed" };
String key = "Chair";
boolean flag = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
System.out.println("Item Available at index "+ i);
flag = true;
}
}
if (flag == false) {
System.out.println("Item Not found");
}
}
}
Output:
Item Available at index 1
We have a String array with three members in the code above: Table, Chair, and Bed. Now we're on the lookout for the Banana. Our output is Chair, which can be found at index location 1.
Sorting
Sorting a String array implies arranging the components in lexicographic order, either ascending or descending.
We can do this using the built-in sort() method or by writing our own sorting algorithm from scratch, but for the sake of simplicity, we'll utilize the built-in way.
class GFG {
public static void main(String[] args)
{
String[] arr = { "Apple", "Cap", "Basketball",
"Car", "Biscuit", "Alto" };
// sorting the array of string
Arrays.sort(arr);
for (String i : arr) {
System.out.print(i + " ");
}
}
}
Output:
Apple Alto Basketball Biscuit Car Cap
Our String array is unsorted, thus following the sort operation, it is sorted in the very same way that a dictionary is sorted, or in lexicographic order.
String Array to String:
When we have to convert String array to string, we can simply use toString() method.
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
String[] arr
= { "My", "name", "is", "Steve Jobs" };
// converting to string
String s = Arrays.toString(arr);
System.out.println(s);
}
}
Output:
[My, name, is, Steve Jobs]
The String array is transformed to a string and stored in a string type variable, but it's worth noting that the string also contains commas(,) and brackets. The code snippet below can be used to generate a string from a string array without them.
public class GFG {
public static void main(String[] args)
{
String[] myarr
= { "My", "name", "is", "Steve Jobs" };
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);
}
}
Output:
My name is Steve Jobs
An object of the StringBuilder class is used in the given code. That is appended to each element of the string array (myarr). The content of the StringBuilder object is then stored as a string using the function toString() method.