- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 17
Lesson 17: Arrays of string type in Java
In Lesson 14 and Lesson 15, we learnt how we can work with arrays of number values and how we can sort and manipulate arrays of number values using loops.
In some cases in Java programming, you would need to store values of string type in an array like days of the week or Boolean (True or False) values. In this article, we shall learn how we can implement and work with arrays of string type in Java.
Arrays of string type in Java are very similar to arrays of int type. Let us declare an array of string type, assign values and then discuss about it.
Here is an example:
String[ ] myString = new String[7] ;
myString[0] = "Here";
myString[1] = "is";
myString[2] = "an";
myString[3] = "array";
myString[4] = "of";
myString[5] = "string";
myString[6] = "type";
The above array, just like we did with number arrays, has a defined array size. The size here is seven positions. We have also assigned values to each of these seven positions. Notice that to assign values of string type we have to enclose those values in double quotes.
To be able to display the values of a string array on the console, we are going to use a FOR loop that will go through the entire array, picking the values and outputting them. The statement will look like shown below:
int i;
for ( i=0; i < myString.length; i++ ) {
System.out.println( myString[i] );
}
The above FOR loop will loop through the string array from index 0 and output the values of the array until when the counter i is less than length of the array. Length is a property of an array that actually return the length of the array. Write the following program and run to see the output.
Sorting a string array in Java
Again, for us to be able to sort the array, we need to import Java sorting inbuilt method using the following statement:
import java.util.Arrays;
The above statement should be placed just after the package name and before the class name. Then, we use the following statement just before we display the values from the array:
Arrays.sort( myString );
For you to be able to see a complete alphabetical sort of the values from the array, the values in the array must all be in lowercase. Here is the sorting program, try it.
Change the values of the array to be all uppercase and try to sort again.
In the next lesson, we shall look at Array lists and how we can use some methods of array lists to manipulate array values.
<< Lesson 16 | Lesson 18 >>
Related hubs...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 4
In Lesson 3, we learnt how to write a Java program in the Java code window and how to Run a Java program. In this lesson, we shall learn how to work with variables in Java and how to manipulate computer memory using variables. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 2
In this lesson you will learn how to create your first Java project and how to interpret the NetBeans code editor window. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 9
In this lesson we shall learn how to use the Switch statement in Java programming. Switch statement is a selection statement, that means when used, it select one value among many values. Switch statement can also be used instead of IF .. ELSE ... - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 6
In this lesson we’ll learn about another useful class for accepting user inputs and displaying results. This class is called JOptionPane class and is located in the javax.swing library.