A one-dimensional array is a list of similar-typed variables. The general form of a one-dimensional array declaration is:
type var-name[ ];
type
declares the array type. type
also determines the data type of each array element.The following declares an array named days with the type "array of int":
int days[];
days
is an array variable. null
.
You allocate memory using new
and assign it to array variables.
new
is a special operator that allocates memory.
The general form is:
arrayVar = new type[size];
type
specifies the type of data being allocated.size
specifies the number of elements.arrayVar
is the array variable.The following two statements first create an int type array variable and then allocate memory for it to store 12 int type values.
int days[];
days = new int[12];
new
.
public class Main {
public static void main(String[] argv) {
int days[];
days = new int[12];
days[1] = 28;
System.out.println(days[1]);
}
}
It is possible to combine the declaration of the array variable with the allocation of the array itself.
int month_days[] = new int[12];
Element Type | Initial Value |
---|---|
byte | 0 |
int | 0 |
float | 0.0f |
char | '\u0000' |
object reference | null |
short | 0 |
long | 0L |
double | 0.0d |
boolean | false |
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |