编程练习生的第七天

容器

  • Collection 容器的父接口
  • 存储每一数据都是一个值的容器
  • 一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。
  • 容器:任意类型数据可以存储,可变长,容器内部存储的数据要求全部为引用数据类型
    /
    public class Collection02 {
    public static void main(String[] args) {
    //接口多态,col能够调用功能,都必须是Collection中存在的
    Collection col=new ArrayList();
    /
    方法
    • boolean add(E e)
      确保此 collection 包含指定的元素(可选操作)。
      boolean addAll(Collection<? extends E> c)
      将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
      void clear()
      移除此 collection 中的所有元素(可选操作)。
      boolean contains(Object o)
      如果此 collection 包含指定的元素,则返回 true。
      boolean containsAll(Collection<?> c)
      如果此 collection 包含指定 collection 中的所有元素,则返回 true。
      boolean equals(Object o)
      比较此 collection 与指定对象是否相等。
      int hashCode()
      返回此 collection 的哈希码值。
      boolean isEmpty()
      如果此 collection 不包含元素,则返回 true。
      Iterator iterator()
      返回在此 collection 的元素上进行迭代的迭代器。
      boolean remove(Object o)
      从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
      boolean removeAll(Collection<?> c)
      移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
      boolean retainAll(Collection<?> c)
      仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。
      int size()
      返回此 collection 中的元素数。
      Object[] toArray()

容器的遍历

Collection

  • 1.增强for循环
  • 2.迭代器
    */
    public class Collection03 {
    public static void main(String[] args) {
    //增强容器的稳定性,可读性
    //强制要求容器中的所有数据的数据类型,不满足泛型就报错
    Collection col=new ArrayList();
    col.add(“因为”);
    col.add(“所以”);
    col.add(“科学”);
    col.add(“道理”);

//增强for
for(String s:col){
System.out.println(s);
}

//迭代器 Iterator iterator() 返回在此 collection 的元素上进行迭代的迭代器。
//凡是Collection实现类的对象都能调用iterator获取操作当前这个容器对象的迭代器
//1)获取迭代器对象
Iterator it=col.iterator();
//2)判断是否还有下一个数据.存在返回true,不存在返回false
while(it.hasNext()){
//3)获取下一个数据
System.out.println(it.next());
}
}

LIST

  • List 有序的 collection(也称为序列)。
  • 有序的可重复的
  • Set 无序的,不可重复
  • List接口新增的方法操作索引的功能
  • add(index,值), indexOf(Object),remove(索引),set(index,数据),get(索引)
public class ListDemo04 {
 public static void main(String[] args) {
  List<Integer> ls=new ArrayList();
  ls.add(10);
  ls.add(12);
  ls.add(12);
  ls.add(13);
  ls.add(1,1);
  System.out.println(ls);
  System.out.println(ls.indexOf(12));
  System.out.println(ls.get(3));
  System.out.println(ls.set(1,2));
  System.out.println(ls.set(0,1));
  System.out.println(ls);
  //当数据也是整数时候,优先以索引为主
  ls.remove(1);
  System.out.println(ls);
  
  //普通for
  for(int i=0;i<ls.size();i++){
   System.out.println(ls.get(i));
  }

LIST存放数组
先上代码

class  SuperMan{
 List<String> is=new ArrayList();    // 创建一个容器
 public void add( ) {
  	name a = new name();
 	for(int i=0;i<a.arg.length;i++){
   	is.add(a.arg[i]);
 }
 }
 	public void superWomen()
 {
  	if(is.contains("灭霸") == true)
 {
   	is.add("惊奇队长");
   	System.out.println("惊奇队长前来救场");
   	System.out.println(is);
 }
 }
 }
 class name{
 
 
 String a="钢铁侠";
 String b="美国队长";
 String c="绿巨人";
 String d="雷神托尔";
 String e="黑寡妇";
 String f="鹰眼";
 String g="绯红女巫";
 String h="快银";
 String i="幻视";
 String j="战争机器";
 String k="猎鹰";
 String l="冬日战士";
 String m="蚁人";
 String n="黄蜂女";
 String o="黑豹";
 String p="星云  ";
 String q="钢铁侠";
 String r="格鲁特 ";
 String s="火箭浣熊";
 String t="奇异博士";
 String u="卡魔拉";
 String v="星爵";
 String x="灭霸";
 
 String[] arg={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,x};
 
 }

使用for循环将数据填入容器中 is.add(a.arg[i]);

第二种方式

public class Test2 {
  String [] arr = {"钢铁侠","美国队长","绿巨人","雷神托尔","黑寡妇","灭霸","蜘蛛侠"};      // 创建一个字符串数组  里面存放英雄
  List<String> is = Arrays.asList(arr);     // 将

通过Arrays.asLis()方法 直接放入数组

首先,该方法是将数组转化为list。有以下几点需要注意:
  (1)该方法不适用于基本数据类型(byte,short,int,long,float,double,boolean)
  (2)该方法将数组与列表链接起来,当更新其中之一时,另一个自动更新
  (3)不支持add和remove方法
通过查看Arrays类的源码可以知道,asList返回的List是Array中的实现的内部类,而该类并没有定义add和remove方法.
另外,为什么修改其中一个,另一个也自动获得更新了,因为asList获得List实际引用的就是数组 。

													2019.07.14
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值