String Array in Java

Introduction

An array is a collection of homogeneous forms of data that are stored in a series of memory locations and can individually be retrieved using their respective indexes in programming.

We have a String data type in the Java programming language. The string is only an object that represents a list of char values. In Java, strings are immutable. In Java, immutable refers to strings that cannot be changed.

It is known as a String Array in Java when a String array is created.

A String array must first be declared and initialized before it can be used. There are several options available for doing this.

Declaration

String arrays can be declared in programs either with or without size. Here is the equivalent code:

String[] String0; // without size
String[] String1=new String[10]; //with size

One String array (String0) without size and another (String1) with a size of four have been declared in the code above. Both of these approaches can be used to declare a String array in Java.

Initialization

//first method
String[] array0=new String[]{"Apple","Banana","Orange","Grapes","Pineapple"};

//second method
String[] array1={"Apple","Banana","Orange","Grapes","Pineapple"};

//third method
String[] array2=new String[5];
array2[0]="Apple";
array2[1]="Banana";
array2[2]="Orange";
array2[3]="Grapes";
array2[4]="Pineapple";

In the first approach, the values are declared on the same line. The last method first creates the String array with the size before we store data in it, and the second method is a condensed version of the first method.

write your code here: Coding Playground

Iteration

A looping statement can be used to iterate through a String array.

// Java program to demonstrate the various
// methods to iterate over a string array

public class string_array {
public static void main(String[] args)
{
String[] array = { "Apple", "Banana", "Orange", "Grapes", "Pineapple" };

// First method
for (String i : array) {
System.out.print(i + " ");
}
System.out.println();

// Second method
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();

// Third method
int i = 0;
while (i < array.length) {
System.out.print(array[i] + " ");
i++;
}
System.out.println();
}
}

Output:

Apple Banana Orange Grapes Pineapple
Apple Banana Orange Grapes Pineapple
Apple Banana Orange Grapes Pineapple

write your code here: Coding Playground

Therefore, there are generally three ways to iterate across a string array. Utilizing a for-each loop is the first approach. The second approach uses a straightforward for loop, and the third approach makes use of a while loop. Iterating over Arrays in Java has more information about iterating over arrays.

Searching

A straightforward linear search technique can be used to locate an element in a string array. Here is how to put that into practice:

// Java program to perform the searching
// operation on a string array

public class string_array {
public static void main(String[] args)
{
String[] array1 = { "Apple", "Banana", "Orange", "Grapes", "Pineapple" };
String key = "Grapes";
boolean flag = false;

for (int i = 0; i < array1.length; i++) {
if (array1[i] == key) {
System.out.println("Available at index "+ i);
flag = true;
}
}
if (flag == false) {
System.out.println("Not found");
}
}
}

Output:

Available at index 3

write your code here: Coding Playground

The String array in the code above has five elements: Apple, Banana, Orange, Grapes, and Pineapple. We are currently looking for a grapes. Our output is Grapes, which is present at index point 3.

Sorting

Sorting a String array involves placing the components in either ascending or downward lexical order.

To do this, we can either develop our own sorting algorithm from scratch or use the built-in sort() method; however, for the sake of simplicity in this post, we will utilize the built-in way.

// Java program to perform the sorting
// operation on a string array

import java.util.Arrays;

class string_array {
public static void main(String[] args)
{
String[] array2 = { "Apple", "Cat", "Ball", "Science",              "Cartoon", "Banana", "Avocado", "Math" };

// sorting the String array
Arrays.sort(array2);

for (String i : array2) {
System.out.print(i + " ");
}
}
}

Output:

Apple Avocado Ball Banana Cartoon Cat Math Science

write your code here: Coding Playground

Our String array, in this case, is not sorted, therefore after the sort operation the array is sorted in lexicographic order, which is the way we used to see it on dictionaries.

String array to String

The function toString() method can be used to convert a String array to a String.

// Java program to demonstrate the
// conversion of String array to String

import java.util.Arrays;

class string_array {
public static void main(String[] args)
{
String[] array3
= { "hi", "!", "you", "are", "learning", "java"};

// converting to string
String s = Arrays.toString(array3);
System.out.println(s);
}
}

Output:

[hi, !, you, are, learning, java]

write your code here: Coding Playground

Here, the String array is transformed into a string and saved in a string type variable, but one thing to observe is that the string also contains brackets and commas (,). Use the code line below to produce a string from a string array without them.

// Java program to demonstrate the
// conversion of String array to String

public class string_array {
public static void main(String[] args)
{
String[] array
= { "hi", "!", "you", "are", "learning", "java" };

StringBuilder sb = new StringBuilder();
sb.append(array[0]);

for (int i = 1; i < array.length; i++) {
sb.append(" " + array[i]);
}

String s = sb.toString();
System.out.println(s);
}
}

Output:

hi ! you are learning java

write your code here: Coding Playground

A StringBuilder class object is present in the code above. For each component of the string array, we are adding that (array). The function toString() method is then used to save the data contained in the StringBuilder object as a string.

Conclusion

A definite number of items of the string type make up a string array. Three methods exist for increasing the number of elements in a string array: pre-allocation, in which the array's size is increased so that values can be added later; construction; and use of the Java ArrayList class of the Collections Framework.