Java One Dimensional (1D) Arrays: Creating Arrays
Creating Arrays
Before creating an array you will need to determine which data type the array should use. The array shown above contains all integer values so this array would naturally be an integer array but you can have an array using any data type (see Java Datatypes and Variables for a comprehensive guide to Java data types). There are a couple of different way to create arrays, they can be created empty or created with values.
Creating Empty Arrays
If you do not know the values that your array will contain at the time you create it you can create an empty array, but you must know how many elements the array will contain. Let’s say you want to create an integer array with 3 elements, you start with defining the data type of the array:
int
Follow the data type with a pair of open and close square brackets [ ] to indicate a one dimensional array:
int [ ]
Next provide a name for the array, and the usual variable naming rules apply here (see Java Datatypes and Variables for more details on variable name rules):
int [ ] arrayName
Next you will need to assign this array to a new instance of an empty array of length 3, do this by first using the new keyword:
int [ ] arrayName = new
Follow that with a repeat of the data type and open/close square brackets, only this time you need to insert the number of elements inside the square brackets:
int [ ] arrayName = new int [3];
We have now created an empty array with 3 elements, ready for use. Later we will explore how to insert and modify data.
This proceedure is used to create empty arrays of any other data type like String, char, double, etc., for example:
String [ ] studentNames = new String [8];
char [ ] studentInitial = new char [10];
double [ ] studentAverageScore = new double [5];
The term “empty array” is a little misleading because although we have not put any values into the array elements they do contain values, but nothing that is useful. Java will fill an empty array with default values relevant to the data type used when creating the array. The follow is a reference table of common default values organised by data type:
Data Type | Default Value |
---|---|
String | null |
int | 0 |
float | 0.0 |
double | 0.0 |
boolean | false |
char | null |
Creating Populated Arrays
If you already know the values that your array will store, the process is similar with a few important differences. Let take the array that holds student grades discussed earlier as the array we will create with values. The first thing you will need to do is state the data type you wish to apply to the entire array:
int
Follow the data type with a pair of open and close square brackets [ ] to indicate a one dimensional array:
int [ ]
Next provide a name for the array, and the usual variable naming rules apply here (see Java Datatypes and Variables for more details on variable name rules):
int [ ] studentGrades
Since we already know which values our array will store we can assign those values to the array using an assignment operator:
int [] studentGrades =
Finally we can add the values making sure each value is seperated with a comma, that the entire range of values are enclosed with a set of curly brackets/braces, and that (like all Java statements) the entire statement is terminated with a semicolon:
int [] studentGrades = {82, 76, 65, 92, 55};
This proceedure is used to create populated arrays of any other data type like String, char, double, etc., for example:
String [ ] studentNames = {“James”, “Barry”, “Frank};
char [ ] studentInitial = {‘J’, ‘B’, ‘F’};
double [ ] studentAverageScore = {82.34, 76.02, 65.33};
Other Legal Array Declarations
More to follow…
Before Moving On
Before we go any further, let’s get this code into your IDE and we will use it to explore some important array properties and methods:
int [] studentGrades = {82, 76, 65, 92, 55};