Using One-Dimensional Arrays in Java

By: Abinaya  

A one-dimensional array is, essentially, a list of like-typed variables. To create an array, you first must create an array variable of the desired type. The general form of a one-dimensional array declaration is

type var-name[ ];

Here, type declares the base type of the array. The base type determines the data type of each element that comprises the array. Thus, the base type for the array determines what type of data the array will hold. For example, the following declares an array named month_days with the type "array of int":

int month_days[];

Although this declaration establishes the fact that month_days is an array variable, no array actually exists. In fact, the value of month_days is set to null, which represents an array with no value. To link month_days with an actual, physical array of integers, you must allocate one using new and assign it to month_days. new is a special operator that allocates memory.

You will look more closely at new in a later chapter, but you need to use it now to allocate memory for arrays. The general form of new as it applies to one-dimensional arrays appears as follows:

array-var = new type[size];

Here, type specifies the type of data being allocated, size specifies the number of elements in the array, and array-var is the array variable that is linked to the array. That is, to use new to allocate an array, you must specify the type and number of elements to allocate. The elements in the array allocated by new will automatically be initialized to zero. This example allocates a 12-element array of integers and links them to month_days.

month_days = new int[12];

After this statement executes, month_days will refer to an array of 12 integers. Further, all elements in the array will be initialized to zero.

Let's review: Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. Thus, in Java all arrays are dynamically allocated. 

Once you have allocated an array, you can access a specific element in the array by specifying its index within square brackets. All array indexes start at zero. For example, this statement assigns the value 28 to the second element of month_days.

month_days[1] = 28;

The next line displays the value stored at index 3.


Putting together all the pieces, here is a program that creates an array of the number of days in each month.

// Demonstrate a one-dimensional array.
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");

When you run this program, it prints the number of days in April. As mentioned, Java array indexes start with zero, so the number of days in April is month_days[3] or 30. It is possible to combine the declaration of the array variable with the allocation of the array itself, as shown here:

int month_days[] = new int[12];

This is the way that you will normally see it done in professionally written Java programs. Arrays can be initialized when they are declared. The process is much the same as that used to initialize the simple types. An array initializer is a list of comma-separated expressions surrounded by curly braces. The commas separate the values of the array elements. The array will automatically be created large enough to hold the number of elements you specify in the array initializer. There is no need to use new. For example, to store the number of days in each month, the following code creates an initialized array of integers:

// An improved version of the previous program.
class AutoArray {
public static void main(String args[]) {
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
System.out.println("April has " + month_days[3] + " days.");

When you run this program, you see the same output as that generated by the previous version.
Java strictly checks to make sure you do not accidentally try to store or reference values outside of the range of the array. The Java run-time system will check to be sure that all array indexes are in the correct range. (In this regard, Java is fundamentally different from C/C++, which provide no run-time boundary checks.) For example, the run-time system will check the value of each index into
month_days to make sure that it is between 0 and 11 inclusive. If you try to access elements outside the range of the array (negative numbers or numbers greater than the length of the array), you will cause a runtime error.

Here is one more example that uses a one-dimensional array. It finds the average of a set of numbers.

// Average an array of values.
fclass Average {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
System.out.println("Average is " + result / 5);

Archived Comments

1. I see your website needs some fresh posts. Writing manually takes
a lot of time, but there i

View Tutorial          By: BaileyX at 2017-09-14 22:21:51

2. Hello sir

this is very nice article about array. this article is very helpful for me

View Tutorial          By: Anurag Singh at 2017-05-10 16:56:43

3. write a code fragment that reverses the order of a one dimensional array a[ ] of double values .

View Tutorial          By: dev at 2017-03-10 10:42:43

4. give me some important and useful example of array in java..,
View Tutorial          By: Pankaj kumar at 2015-11-02 09:09:54

5. Write a program using one-dimensional array using function that determines the lowest value among th
View Tutorial          By: Ike Conte at 2015-09-22 10:49:28

6. Write a program using one-dimensional array using function that determines the lowest value among th
View Tutorial          By: Ike Conte at 2015-09-22 10:47:50

7. Can you help me input 15 states and capitals in 2 single dimension arrays and then print the corresp
View Tutorial          By: Hardik Negandhi at 2015-06-24 11:18:51

8. I need you to Write me a program to initialize one dimensional with the name of Array with 10 elemen
View Tutorial          By: Sayed maseer at 2015-06-20 05:09:12

9. Thanks ,It is very useful...
View Tutorial          By: SABARISH at 2014-08-08 05:46:43

10. boss y do u copy from Complete reference of java2(Herbert Schildt) ????????? :(
View Tutorial          By: rahul vaidya at 2011-12-30 13:33:46

11. writa a java program to add two one-dimensional arrays
View Tutorial          By: nibedita swain at 2011-08-22 15:23:37

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial