![]() |
VOOZH | about |
A String Array in Java is an array that stores string values. 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.
To iterate through a String array we can use a looping statement. So generally we have three ways to iterate over a string array.
Apple Banana Orange Apple Banana Orange Apple Banana Orange
To find an element from the String Array we can use a simple linear search algorithm.
Available at index 1
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.
Apple Avocado Ball Banana Cartoon Cat
To convert from String array to String, we can use a Arrays.toString() method or a custom approach.
[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.
To create a string from a string array without comma(,) and brackets , we can use the below code snippet.
She is a good girl
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,
str1 is declared with a size of 4.We can use both of these ways to declare String array in Java.
In this method, we are declaring the values at the same line.
String[] arr0 = new String[]{"Apple", "Banana", "Orange"};
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"};
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";