day03 数组

 * 动态初始化 :动态数组     int  grade[] =  new   int [student_count]; 
              静态数组
 * 数据类型[] 数组名 = new 数据类型[指定数组长度] ;
 *  数据类型 数组名[] = new 数据类型[指定数组长度] ;
 *  
 *  
 *  举例:int[] arr = new int[3] ;
 *   int arr[] = new int[3] ;
 *
public class ArrayDemo4 {
public static void main(String[] args) {
int[] arr1 = new int[3] ;
System.out.println(arr1);
System.out.println(arr1[0]);
System.out.println(arr1[1]);
System.out.println(arr1[2]);
int[] arr2 = new int[2] ;
System.out.println(arr2);
System.out.println(arr2[0]);
System.out.println(arr2[1]);
//给元素赋值
arr1[0] =  100 ;
arr1[2] = 50 ;
arr2[0] = 30 ;
//定义第三个数组
int[] arr3 = arr1 ;
arr3[0] = 200 ;
arr3[2] = 70;
System.out.println(arr3);
System.out.println(arr1[0]);//200————— arr3[0]把arr1[0]覆盖住了
System.out.println(arr1[1]);//0
System.out.println(arr1[2]);//70
}
}

 * 运行时期异常:Runtime Exception
 * java.lang.ArrayIndexOutOfBoundsException:数组角标越界异常!
 * 出现这个异常的原因:访问了数组中不存在的索引
 * 
 * 运行时期的异常:
 * java.lang.NullPointerException:空指针异常:
 * 出现这个异常的原因:数组的对象为空,都不执行内存地址值,还要输出元素.
//空指针异常的解决办法:非空判断
if(arr != null){
System.out.println(arr[0]);
}
——————————————————————————————————————————————
package 数组;

public class 数组遍历 {
public static void main(String[] args){
int arr[]={0,1,2,3};
for(int x=0;x
System.out.println(arr[x]);}
}
}
最大值
package 数组;

public class 最大值 {
public static void main(String[] args){
int arr[]={0,1,2,3,5};
int max = arr[0];
int x;
for (x=1;x
if(arr[x]>max){
max=arr[x];
}
}
System.out.println("最大值是"+max);
}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值