java集合类collection的用法(二)

public class  Text10{

     public static void main(String[] args) {
    //集合一   
         //创建集合对象
    Collection c = new ArrayList();

    c.add("你好");
    c.add("加油");
    //集合二

// Collection c1 = new ArrayList();
c.add(“努力”);
c.add(“勤奋”);
//Object objs =”hello” 向上转型
//object []toArray();把集合转换数组,可以实现集合的遍历
Object []objs =c.toArray();
for (int i = 0; i < objs.length; i++) {
String s = (String) objs[i];
System.out.println(s+”===”+s.length());
}
// System.out.println(Object[i]);
//我知道元素是字符串,我在获取到元素的同时,害羞真的元素的长度
// System.out.println(objs[i]+”===”+objs[i].length());
//上面的实现不了,原因是Object中没有length()方法
// 我们想使用字符串的方法,就必须能把元素还原字符串
集合类Collection的遍历数组
public class Student {

private String name;
private int age;

public Student(String string, int i) {
    super();
    this.age =age;
    this.name=name;
}

public static void main(String[] args) {

}

public void setName(String name) {
    this.name = name;
}

public void setAge(int age) {
    this.age = age;
}



public int getAge() {

    return age;
}

public String getName() {

    return name;
}

  public class  Text10{

     public static void main(String[] args) {
    //集合一   
         //创建集合对象
    Collection c = new ArrayList();

    //创建学生对象
    Student  s1 = new Student("小明",22);
    Student  s2 = new Student("小红",23);
    Student  s3 = new Student("小花",25);
    Student  s4 = new Student("小宇",25);
    Student  s5 = new Student("小伟",27);
    //把学生类添加到集合
    c.add(s1);
    c.add(s2);
    c.add(s3);
    c.add(s4);
    c.add(s5);
    //把集合转换数组
    Object []objs =c.toArray();
    //遍历数组
    for (int i = 0; i < objs.length; i++) {
        Student s = (Student) objs[i];
    System.out.println(s.getName()+"==="+s.getAge());
    }

public class Text10{
//获取元素 Iterator的用法
// Object next()
public static void main(String[] args) {
//集合一
//创建集合对象
Collection c = new ArrayList();
//添加元素
c.add(“你好”);
c.add(“加油”);
c.add(“努力”);
c.add(“勤奋”);

   //Iterator iterator() 迭代器,集合的遍历方式
    /* boolean hasNext()   如果仍有元素可以迭代,则返回 true。
     *  E next() 返回迭代的下一个元素。
     *  void remove()   从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。
     * 获取元素时先判断,再获取 否则会报错
     */
    Iterator it =c.iterator();
    //hasNext的用法 获取所以元素
    while (it.hasNext()) {
        String s =(String)it.next();
        System.out.println(s);
    }
    //next的用法  获取一个元素
    Object  obj = it.next();
    System.out.println(obj);
    //获取多个 需要加上判断
    if (it.hasNext()) {
        System.out.println(it.hasNext());
    }
    if (it.hasNext()) {
        System.out.println(it.hasNext());
    }
    if (it.hasNext()) {
        System.out.println(it.hasNext());
    }   

List的遍历用法
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

 public class  Text10{

     public static void main(String[] args) {
    //集合一   
         //创建集合对象
    Collection c = new ArrayList();
    List list = new ArrayList();
    //添加元素
    c.add("你好");
    c.add("加油");
    c.add("努力");
    c.add("勤奋");
Iterator it =c.iterator();
    //hasNext的用法 获取所以元素
    while (it.hasNext()) {
        String s =(String)it.next();
        System.out.println(s);
    }
发布了37 篇原创文章 · 获赞 53 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_40861561/article/details/80027163
今日推荐