Introduction to String Array in Java
Java string array is like any other string function in Java, which is used for doing String manipulations. As the name suggests, the array is used for storing homogenous groups of data, i.e. either all the data stored inside it are of String type, or double type, or int type, etc. Therefore, string array is the data structure in java used to store the string type values and is used to hold a fixed number of string values. This is one of the very basic levels of data structures that are most commonly used in the Java programming language. When you talk about the Java main method, even that method is of the string array type.
Declaration
Declaration of string array is useful as without declaration of a variable in java; it is not possible to use it. There are multiple ways of a declaration of the string arrays in java, without instantiation of an object and along with object references.
Let us look at the ways of a declaration of string arrays.
String [] strArr; //This is declaration of strArr array without specifying size
String[] strArr1 = new String [3]; //This is the declaration of strArr array with specifying string type of size 3
In this case, the String [] strArr can also be written as String strArr[], but the former one is the more recommended and a standard way of approaching the syntax.
Another point to be noted in both kinds of declarations is that the former value returns the value as null, whereas the latter value returns the value as [null, null, null] as the size is 3.
Initialization of String Type Array in Java
Once the variable or the data structure is declared, the next step is the initialization of a string type array. This is useful when a fixed static value is initialized. The array can also dynamically initialize the value, and it all depends upon the requirement.
Dynamic allocation is more helpful when the user input is to be determined at the run time, whereas if you are writing the program with the perspective of prototyping or just to get a flavor of the code written, static initialization of the string type array can also work.
Let us try to understand the concept of initialization with the help of the above two examples, which are just explained in the case of declaration of array string variables.
Example:
String [] strArr= {"X", "Y", "Z"};
strArr1[0]="X";
strArr1[1]="Y";
strArr1[2]="Z";
String [] strArr2= new String [] {"X", "Y", "Z"};
If we start to compare two string object values in the above example, they would return a result as false. The reason for such a behavior is that the arrays are objects, and the object class makes use of this as the current object value and hence the value false. But on the other hand, if the values being compared are actual values and not the objects, then the result of true will be returned.
Sorting a String Array in Java
Before we try to understand how sorting is done in Java, let us first understand what sorting actually means. Sorting means arranging the values of the array in a particular fashion, either in ascending order or in descending order. We can implement the sorting technique by writing our own custom sorting function or using the Array class sorting method.
Example:
String [] letters= {e, d, c, b, a};
System.out.println("Values before sorting" + Array.toString(letters));
Arrays.sort(letters);
System.out.println("Values after sorting" + Array.toString(letters));
Output:
One point to be noted in this case is that the string implements the comparable interface, and therefore this thing works in the case of natural sorting. Arrays.sort() can also be used to sort the values of the array in some other way by making use of a comparator. Writing custom sort function is also an option, but it should only be used when there is a prohibition on the use of the already built-in library sort function as it is compiler intensive. Two majorly popular methods of doing sorting in the case of Java is bubble sort and selection sort. We are not going into detail about these algorithms as that would be out of this article’s scope.
How to Search a String from an Array of String?
If you wish to search a particular string from an array of string, we can make use of a looping construct or substring. When the string is comparatively smaller than the original string, then the substring function can be made to put to use. If in case the string is complex than the actual string, then the looping construct can be used to search a string within a string.
Let us understand all this searching of a string with the help of an example.
Example:
import java.io.*;
public class SearchStringEG {
public static void main(String args[]) throws IOException {
String[] strArr = { "X", "Y", "z" };
boolean flag = false;
String s = "Y";
int ind = 0;
int l =strArr.length();
int i;
for (i = 0; i < l; ++i) {
if(s.equalsIgnoreCase(strArr[i])) {
ind = i;
flag = true;
break;
}
}
if(flag==true)
System.out.print ("String " + s +" is found at index position "+ind);
else
System.out.print("String " + s +" is found at index position "+ ind);
}
}
Output:
In the above example, we have introduced a new keyword break that ensures that the program’s loop is maintained until the break keyword is applied. This keyword works on both loops as well as conditional statements such as if-else. Therefore the moment we are able to find our string, we take an exit using the break keyword. Two very popular algorithms used for searching in Java is binary search and linear search.
Recommended Articles
This is a guide to String Array in Java. Here we discuss the string array in java and various sorting as well as searching methods related to it along with initialization of string type array in java. You may also look at the following articles to learn more –