
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
String Arrays in Java
String and Arrays are two distinct things. Array is a linear data structure that is used to store group of elements with similar datatypes but a string is a class in Java that stores a series of characters. Those characters are actually String-type objects. Since strings are objects we can say that string arrays are group of string-type objects.
In this article, we will understand string arrays in java and perform some operations on them.
String Arrays
The string array is a combination of array and string both. Therefore, it has the properties of both array and string such as ?
It stores data in a sequential manner like an array.
Once we create an array we can't change its size i.e. it is of fixed length.
The values are enclosed within double quotes.
Strings are immutable in java, so string arrays are too. Here, immutable means the elements of a string array cannot be modified.
Syntax
String[] nameOfarray; // declaration Or, String nameOfarray[]; // declaration Or, // declaration with size String nameOfarray[] = new String[sizeofarray]; // declaration and initialization String nameOfarray[] = {values within double quotes separated with comma};
We can use any of the above syntaxes in our program. Instance ?
In this example, we will declare three arrays ?st1', ?st2' and ?st3'. Also, we will declare and initialize an array named ?st4'.
String[] st1; // declaration String st2[]; // declaration String st3[] = new String[10]; // declaration with specified size // declaration and initialization String st4[] = { "values", "within", "double", "quotes" };
We can initialize at the time of declaration or we can declare first and initialize with values whenever we require in the program. However, at the time of declaration the variables get initialized with the default values and ?null' is the default value of the string array.
Example 1
The following example illustrates what will happen if we don't initialize the string array.
public class Main { public static void main(String[] args) { String st1_arr[] = new String[3]; System.out.println("Elements of the given array: "); System.out.print( st1_arr[0] + " " + st1_arr[1] + " " + st1_arr[2] ); } }
Output
Elements of the given array: null null null
As discussed earlier, we can see that ?st1_arr' is initialized with ?null' value. All the indexes of this string array contains ?null' only.
Let's discuss a few operations we can perform on string arrays
Accessing the elements of String Arrays
We can access all the elements by iterating through the given array using iterative approaches like for, for each and while loop.
Here we will use for each loop to access string array elements.
Syntax of for each loop
for(Data_Type nameOfvariable : nameOfarray) { // your code will come here }
Parameters
Data_Type ? primitive datatype of the given array.
nameOfvariable ? variable to which the value of array is going to reassign.
nameOfarray ? name of the given array.
Example
public class Acces { public static void main(String[] args) { String st1_arr[] = {"Laptop", "Desktop", "Tablet", "Smartphone", "Smartwatch"}; System.out.println("Elements of the given array: "); for(String val : st1_arr){ System.out.print( val + " "); } } }
Output
Elements of the given array: Laptop Desktop Tablet Smartphone Smartwatch
In the above code, elements of string array ?st1_arr' are getting stored in variable ?val' sequentially and then they are printed.
To count number of elements in string array
We will create a string array and a counter variable to store the number of elements. Using for loop we will iterate through the elements and after each iteration we will increment the counter variable by 1.
Example
public class Count { public static void main(String[] args) { String st_arr[] = {"Tutorials", "point", "and", "Tutorix"}; int counter = 0; for(int i = 0; i < st_arr.length; i++) { counter++; } System.out.print("Number of elements in the given string array: " + counter); } }
Output
Number of elements in the given string array: 4
To search a particular element
We will search the value stored in variable ?key'. In the for loop we will iterate through all the elements and using ?equals()' method we will check whether the given key is available in array or not. If it is available then ?isFound' variable gets incremented by 1 and the if block will return the index value of ?key'.
Example
public class Srch { public static void main(String[] args) { String st_arr[] = {"Tutorials", "point", "and", "Tutorix"}; String key = "Tutorix"; int isFound = 0; int position = 0; for(int i = 0; i < st_arr.length; i++) { if(key.equals(st_arr[i])) { isFound = 1; position = i; } } if(isFound == 1) { System.out.print("Element is available at index: " + position); } else { System.out.print("Element is not available"); } } }
Output
Element is available at index: 3
Conclusion
In this article, we have learned about string arrays, discussed how we can create them and also performed some operations on the string array such as searching, accessing and counting the number of elements.