java array initialization

the method of initialization 

1 static

type[] name;

name=new type[]{e1,e2,,};

or:

type[] name={e1,e2,,};

2.dynamic

type[] name;

name=new type[n];

or

type[] name=new type[n];




1.1

public class helloworld 
{
public static void main(String[] args) 
{
String[] app;
app=new String[]{"hello","world"};

int count=0;
while (count<app.length)
{
System.out.println(app[count]);
count++;
}

}
}

1.2

public class helloworld 
{
public static void main(String[] args) 
{
String[] app={"hello","world"};
int count=0;
while (count<app.length)
{
System.out.println(app[count]);
count++;
}

}
}

1.3

public class helloworld 
{
public static void main(String[] args) 
{
int[] app;
app=new int[5];

int i=0;
while (i<app.length)
{
app[i]=i;
System.out.println(app[i]);
i++;
}

}
}

1.4

public class helloworld 
{
public static void main(String[] args) 
{
int [] app=new int[5];
int i=0;
while (i<app.length)
{
app[i]='a'+i;
System.out.println(app[i]);
i++;
}

}
}



2.

public class helloworld 
{
public static void main(String args[]) {  
         Date days[] ;  //Date[] days;is preferable
         days = new Date[3] ;  

         days[0] = new Date(2015,3,20) ;  
         days[1] = new Date(2008,2,31) ;  
         days[2] = new Date(2008,4,4) ;
int i=0;
while (i<days.length)
{
System.out.println(days[i].year+"\\"+days[i].month+"\\"+days[i].day);
i++;
}
     }  
}  
 
class Date  
{  
         int year,month,day ;  
         Date(int year ,int month ,int day)
{  
         this.year = year ;  
         this.month = month ;  
         this.day = day ;  
        }  
}



3.

public class helloworld 
{
public static void main(String args[]) 
{
Date days[]={new Date(2009,10,25),new Date(2010,3,23),new Date(2001,2,5)};// Date[] days=....
 
int i=0;
while (i<days.length)
{
System.out.println(days[i].year+"\\"+days[i].month+"\\"+days[i].day);
i++;
}
}
}  
 
class Date  
{  
int year,month,day ;  
Date(int year ,int month ,int day)
{  
this.year = year ;  
this.month = month ;  
this.day = day ;  
}  
}

 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值