Java 数组 类集框架

Java中数组的声明分为静态和动态两种
静态声明:
int a[] = {1,2,3,4}; 
动态声明:
int a[] = new int[10];
其中方括号的位置可以在a前面也可以在a后面,但是要固定一种模式,不能一会儿在前一会儿在后。




二维数组:数组的元素还是数组
也分为静态和动态两种
静态:int a[][] = {{1,2,3},{4,5,6},{7,8,9}};
动态:int a[][] = new int[3][4];


数组的大小一旦固定就不可更改,为了能够更改大小,Java中引入了类集框架


类集框架:一系列的类和接口,在java.util包中。
分为三类:集合(set),列表(list),映射(map)
继承实现关系:
Iterator(接口)->Collection(接口)->Set(接口)& List(接口) HashSet是Set的实现类,ArrayList是List的实现类
Map(接口)  HashMap为其实现类
1集合
特点:对象没有顺序,无重复对象。
public class TestSet {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("a");
set.add("b");
set.add("c");
set.add("a");
System.out.println(set.size());
Iterator<String> it =set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}


长度为3,因为对象值不能重复
2列表
特点:顺序存储,可以有重复值
注意:不按照顺序添加数据会报告异常,如果把下面代码中的第一个3改为4,就会报错
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();

arr.add(0,"h");
arr.add(1,"a");
arr.add(2,"b");
arr.add(3,"a");
arr.add(3,"c");

int length  = arr.size();

System.out.println(length);
for(int i=0;i<arr.size();i++){
System.out.println(arr.get(i));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值