<h1>集合类</h1> | |
<ul> | |
<li> | |
为什么会有集合类? | |
<ul> | |
<li>集合类是容器,计算机描述各宗各样的类,需要存区操作</li> | |
</ul> | |
</li> | |
<li> | |
集合类和数组的区别 | |
<ul> | |
<li>数组有长度,而且是同种类型;</li> | |
<li>集合不是。</li> | |
</ul> | |
</li> | |
<li>集合是一种框架,它的有一些公有的方法。这些方法定义成一个Collection接口</li> | |
<li> | |
Collection 接口常用的方法 | |
<ul> | |
<li> | |
添加方法 | |
<ul> | |
<li>boolean add(Object o);加入一个元素</li> | |
<li>boolean addAll(Collection):将一个参数容器中的元素添加到当前容器中。</li> | |
</ul> | |
</li> | |
<li> | |
删除方法 | |
<ul> | |
<li>boolean remove(Object)//删除一个指定方法</li> | |
<li>boolean removeAll(Collection):删除指定的collection中和本Collection中相同的元素</li> | |
</ul> | |
</li> | |
<li> | |
判端方法 | |
<ul> | |
<li>boolean contains(object):是否包含指定元素</li> | |
<li>boolean containsAll(Collection):是否包含指定容器中的元素</li> | |
<li>boolean isEmpty():是否有元素</li> | |
</ul> | |
</li> | |
<li> | |
长度 | |
<ul> | |
<li>int size():获取的几个元素</li> | |
</ul> | |
</li> | |
<li> | |
取交集 | |
<ul> | |
<li>boolean retainAll(collection):保留和指定collection集合中相同的元素,不同的元素会删除</li> | |
</ul> | |
</li> | |
<li> | |
将集合装换成数组 | |
<ul> | |
<li>Object [] toArray(); </li> | |
</ul> | |
</li> | |
<li>取出元素</li> | |
<li>Inteator itarator</li> | |
</ul> | |
</li> | |
</ul> | |
<p>迭代器:取出集合中的元素的方式。该对象本身内置在容器中</p> | |
<p>如何获取到集合的迭代器?</p> | |
<p>可以通过iterator()方法来完成,该方法返回一个Iterator类型对象。</p> | |
<pre><code>import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Iterator; | |
public class IteratorDemo { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
//创建集合 | |
Collection coll = new ArrayList(); | |
//添加元素 | |
coll.add("abc1"); | |
coll.add("abc2"); | |
coll.add("abc3"); | |
//获取改集合的迭代器对象 | |
//Iterator it =coll.iterator(); | |
//使用迭代器对象中的方法完成元素的获取。 | |
//System.out.println(it.next()); | |
//System.out.println(it.next()); | |
/*while(it.hasNext()){ | |
System.out.println(it.next()); | |
}*/ | |
//for循环结束,可以把迭代器对象消失 | |
for(Iterator it1 = coll.iterator(); it1.hasNext();){ | |
System.out.println(it1.next()); | |
} | |
} | |
} | |
</code></pre> | |
<p>迭代器的原理</p> | |
<ul> | |
<li>取值的时候先判断里是否有元素。</li> | |
<li>容器很多,因为每个容器机构不同的,</li> | |
<li>迭代器出现的接口出现:将每个容器中的取出方式进行分装。并对外暴露。</li> | |
<li>这样无论是什么容器或者数据结构,只要内部取出方式实现了Interator接口</li> | |
<li> | |
<p>都可以通过该接口取出这些容器中的元素</p> | |
</li> | |
<li> | |
<p>它的出现将数据的取出方式和容器的数据结构相分离,降低了耦合性。</p> | |
</li> | |
<li>而取出方式因为直接在访问器中的元素,并依赖具体的数据结构,所以被定义在了容器中。</li> | |
<li>通过内部类现实Iterator接口</li> | |
<li> | |
<p>package liwei.www.p1.stringbuffer;</p> | |
<p>public class Person {</p> | |
<pre><code>private String name; | |
private int age; | |
Person(String name,int age){ | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public String toString(){ | |
return "Person["+name+ "......"+age+"]"; | |
} | |
</code></pre> | |
<p>}</p> | |
<p>import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Iterator;</p> | |
<p>public class CollectionDemo2 {</p> | |
<pre><code>/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
/*集合一些小细节 | |
* | |
*/ | |
//创建一个集合容器,其实也是一个对象 | |
//这个对象中存储着特定结构可以对对象进行存储。 | |
Collection coll = new ArrayList(); | |
/* | |
* 存储时,add可以接受任意类型的对象因为参数类型是Object | |
* 2其实在集合中真正存储是对象的引用 | |
* | |
*/ | |
coll.add(new Object()); | |
coll.add("abc"); | |
/* | |
* 用迭代器去出时,其实取出的也是引用。 | |
* | |
*/ | |
coll.add(5);//可以在1.5后,有了自动装箱机制。 new Interger(),装箱可以是Interger.valueof(5) | |
coll.add(Integer.valueOf(5));//拆箱用的intValue(); | |
//Iterator | |
for(Iterator it =coll.iterator();it.hasNext();){ | |
System.out.println(it.next()); | |
} | |
} | |
</code></pre> | |
<p>}</p> | |
</li> | |
<li> | |
<p>存储时,都被提升为Object。</p> | |
</li> | |
<li> | |
<p>取出时如果要使用自定义对象的特有方法,一定要进行向下装换</p> | |
</li> | |
</ul> | |
<p>注意事项:在迭代时,循环值,只要有一个next()方法即可</p> | |
<pre><code>package liwei.www.p1.stringbuffer; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Iterator; | |
public class CollectionDemo3 { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
/* | |
* 一般开发时,最常见的就是往集合存储自定义对象。 | |
* 1对自定义对象进行描述 | |
*/ | |
Collection coll = new ArrayList(); | |
coll.add(new Person("lisi",21)); | |
coll.add(new Person("王五",35)); | |
coll.add(new Person("赵六",33)); | |
//取出元素it.next取出的对象都是Object | |
for(Iterator it =coll.iterator();it.hasNext();){ | |
//java.util.NoSuchElementException | |
//System.out.println(((Person)it.next()).getName()+":"+((Person)it.next()).getAge()); | |
Person p = (Person)it.next(); | |
System.out.println(p.getName()+":"+p.getAge()); | |
} | |
} | |
} | |
</code></pre> | |
Collection常用的方法和注意事项
最新推荐文章于 2022-02-11 16:26:02 发布