Java中使用关键字new创建数组对象,格式为:
数组名 = new 数组元素的类型 [数组元素的个数]
实例:
TestNew.java:
程序代码:
public class TestNew
{
public static void main(String args[]) {
int[] s ;
int i ;
s = new int[5] ;
for(i = 0 ; i < 5 ; i++) {
s[i] = i ;
}
for(i = 4 ; i >= 0 ; i--) {
System.out.println("" + s[i]) ;
}
}
}
public class TestD
{
public static void main(String args[]) {
int a[] ;
a = new int[3] ;
a[0] = 0 ;
a[1] = 1 ;
a[2] = 2 ;
Date days[] ;
days = new Date[3] ;
days[0] = new Date(2008,4,5) ;
days[1] = new Date(2008,2,31) ;
days[2] = new Date(2008,4,4) ;
}
}
class Date
{
int year,month,day ;
Date(int year ,int month ,int day) {
this.year = year ;
this.month = month ;
this.day = day ;
}
}
TestS.java(静态):
程序代码:
public class TestS
{
public static void main(String args[]) {
int a[] = {0,1,2} ;
Time times [] = {new Time(19,42,42),new Time(1,23,54),new Time(5,3,2)} ;
}
}
class Time
{
int hour,min,sec ;
Time(int hour ,int min ,int sec) {
this.hour = hour ;
this.min = min ;
this.sec = sec ;
}
}
TestDefault.java(默认):
程序代码:
public class TestDefault
{
public static void main(String args[]) {
int a [] = new int [5] ;
System.out.println("" + a[3]) ;
}
} 作者: zhangyaya 时间: 2015-1-14 10:57
程序差注释