String array in Java
Strings in Java are Objects that are backed internally by a char array. Strings are immutable. Whenever a change to a String is made, a brand new String is created.
The string array is a collection of strings.
Java String array is basically an array of objects.
There are two ways to declare a string array
1). Declare without size
String[] strArray; //declare without size
String[] fruits = null;
2). Declare with size.
String[] strArray = new String[6]; //declare with size
we can initialixe string array by 2 ways
1). Initialize at the time of declaration,
2) Initialize empty array at the time of declaration, and then assign reference later. We can do a different kind of processing on string array such as iteration, sorting, searching, etc.
String Array Initialization
//inline initialization
String[] fruits1 = new String[] { "Apple", "Mang0", "Banana","blackBerry","Kiwi","Dragonfruit" };
String[] fruits2 = { "Apple", "Mango", "Banana","BlackBerry","Kiwi","Dragonfruit" };
//initialization after declaration
String[] fruits3 = new String[6];
fruits3[0] = "Apple";
fruits3[0] = "Mango";
fruits3[0] = "Banana";
fruits3[0] = "BlackBerry";
fruits3[0] = "Kiwi";
fruits30] = "Dragonfruit";
Although when we iterate the array,the content arrya elements are same but.Array are objects.When we compare the array it returns false.
As the object class equals method has a reference comparison mechanism, not a content comparison.
Iterating over a java string array
We can iterate string array using java for loops.
package com.javacodestuffs.core.strings;
public class Program {
public static void main(String[] args) {
// Create three-element String array.
String[] fruits = new String[] {
"Apple",
"Mango",
"Banana",
"blackBerry",
"Kiwi",
"Dragonfruit"
};
System.out.println("fruits array size is : " + fruits.length);
System.out.println("first element from fruits array is : " + fruits[0]);
System.out.println("\nIterate over strings using for-each loop :");
for (String fruit: fruits) {
System.out.println(fruit);
}
String[] languages = new String[] {
"Java",
"Php",
".Net",
"Python",
"Ruby",
"Scala",
"Groovy"
};
System.out.println(languages.length);
System.out.println("\nIterate over strings using for loop : ");
for (int i = 0; i < languages.length; i++) {
System.out.println(languages[i]);
}
}
output:
fruits array size is : 6
first element from fruits array is : Apple
Iterate over strings using for-each loop :
Apple
Mango
Banana
blackBerry
Kiwi
Dragonfruit
7
Iterate over strings using for loop :
Java
Php
.Net
Python
Ruby
Scala
Groovy
Convert String to String Array in Java
We can convert string to String array
we have strings separated by - and we need to convert to it into an array. we have to use the split method to split the string , which gives string array.
String str = "Apple-Banana,Mango";
String[] fruits = str.split("-");
System.out.println(Arrays.toString(fruits)); //[Apple, Bananae,Banana]
String Array Sorting in Java
We can sort string array using Arrays class's sort method. The sorting is done as ascending order.
Arrays.sort(fruits);
package com.javacodestuffs.core.strings;
import java.util.Arrays;
public class Program {
public static void main(String[] args) {
String[] fruits = new String[] {
"Apple",
"Mango",
"Banana",
"blackBerry",
"Kiwi",
"Dragonfruit"
};
System.out.println("Before sorting fruits array : ");
System.out.println(Arrays.toString(fruits));
Arrays.sort(fruits);
System.out.println("\nAfter sorting fruits array : ");
System.out.println(Arrays.toString(fruits));
}
}
output:
Before sorting fruits array :
[Apple, Mango, Banana, blackBerry, Kiwi, Dragonfruit]
After sorting fruits array :
[Apple, Banana, Dragonfruit, Kiwi, Mango, blackBerry]
Java Search elements in the String array
We can iterate each array element and compare each element to the element searched for. If elements found we break the loop.
package com.javacodestuffs.core.strings;
import java.util.Arrays;
public class Program {
public static void main(String[] args) {
String[] fruits = new String[] {
"Apple",
"Mango",
"Banana",
"BlackBerry",
"Kiwi",
"Dragonfruit"
};
boolean found = false;
int index = 0;
String searchStr = "BlackBerry";
for (int i = 0; i < fruits.length; i++) {
if (searchStr.equals(fruits[i])) {
index = i;
found = true;
break;
}
}
if (found) System.out.println(searchStr + " is found at " + index + " location.");
else System.out.println(searchStr + " is not found in the fruits array.");
}
}
output:
BlackBerry is found at 3 location.
Convert String Array to List in Java
We can convert String Array to List using the asList method of Arrays class.
Arrays.asList(vowels);
package com.javacodestuffs.core.strings;
import java.util.Arrays;
import java.util.List;
public class Program {
public static void main(String[] args) {
String[] fruits = new String[] {
"Apple",
"Mango",
"Banana",
"BlackBerry",
"Kiwi",
"Dragonfruit"
};
List < String > fruitsList = Arrays.asList(fruits);
System.out.println("The Fruits List is : " + fruitsList);
}
}
output:
The Fruits List is : [Apple, Mango, Banana, BlackBerry, Kiwi, Dragonfruit]
Post/Questions related to String array in Java
How to return string array in java?
How to store multiple strings in an array in java?
How to take string array input in java?
How to assign values to string array in java?
How to print string array in java?
How to convert a string array to string in java?
How to create dynamic string array in java?
How to declare string array in java?
How do you declare an array of strings in Java?
How do I add values to a string array in Java
In this article, we have seen String array in Java with examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment