Grails Cookbook - A collection of tutorials and examples

Java 5 For Loop String Array

No matter how advanced programming will be in the future, the basics will always be important. Cornerstone are basic data structure such as String Array, which is very simple to understand and use. One typical thing we wish to do with a String Array is to loop through all of it's values. Below is an example on how to loop to all values of a String Array, using for loop introduced in Java 5.

Java String Array Initialization

It is a common scenario that we need to manipulate multiple String values. String Array is always a good choice because it is very simple to work with. The first thing we wish to do when working with arrays is to initialize it with values. We will share below some code examples on how to initialize a String Array in Java.

Convert String Array to List in Java

When we wish to work with multiple values of the same type, the most tempting data structure to use is Array. This provide a simple and fast way to create and manage multiple instances of a Class. It is easy and intuitive to use. However, one frustrating limitation of Arrays is that the number of items it holds is not flexible. Meaning we are stuck with a fixed number of items throughout the lifetime of the data structure. It is not possible to add more items later. That is why if we need flexibility, Lists are much better to work with. To cater the transition from one data structure to another, below are some examples on how to convert a String Array to List in Java.

Sort String Array in Java

String Array is a very useful data structure when we need to hold multiple String values. It is very simple to understand for programmers with background in some other language. It is also easy to use for many programming scenarios. But one common thing we usually need when working with Arrays, is that we sometimes need the data to be ordered or sorted. Below are some examples on how to Sort String Array in Java.

Check If String Array Contains a Value In Java

Arrays are very useful when we need to work with multiple values of the same type. For example, we may need multiple instances of Strings. Arrays provide a data structure that is easy to learn and use. But since arrays are simple, it does not have much methods for some operations we may need. Below are examples on how to test or check if a String Array contains a certain value in Java.

How To Add element to String Array in Java

Working with arrays in Java is common because this data structure is simple to use and very efficient. Most programming language supports arrays, so many developers may have idea on how to use it without reading much documentation. Specially for programmers coming from C or C++ background, the way on how to use it is very similar. But there is one big limitation when working with Arrays in Java, the length or size of Arrays are fixed. Meaning we can not increase or decrease the size without programming tricks. Below are some examples on how we can add element to a String Array in Java.

Two Dimensional String Array in Java

One-dimensional arrays in Java are commonly used in everyday programming. However, there are instances where we need two-dimensional array. An example is holding data for a table listing with multiple columns and rows. Another example is holding information about a board game like chess. Below are some example on how to work with Two Dimensional String Array in Java.

Iterate Through String Array in Java

A String Array is a data structure that holds several String values. The number of String elements is fixed and can't be changed throughout the lifetime of the array object. One common thing that we wish to do with String Array is to go through all the data it contains and process individually. Below contains examples on how to iterate through String Array in Java.

String Array Length in Java

String Array is a commonly used data structure when programming in Java. Although we can generally use List for greater flexibility, but sometimes using String Array is more suitable for a given scenario. Arrays in general have the property of having fixed number of elements and the size can't be changed throughout the lifetime of the data structure. When given a String Array as parameter to our method, it is common that we wish to know the length of it. Below are some examples on how to get the length of a String Array in Java.

Byte Array to String in Java

There are programming scenarios where we have a data in byte array that represents a String. This could be a case when we use a third party library to load data somewhere and the result is a byte array. In such cases, we may want to convert back the byte array to a String. Below are some examples on how to convert a byte array to String in Java.