在上海乐字节学习Java的第十天

重点

1.对象数组

  • Student[] stus = new Student[5];
  • 对象数组缺点:添加一个新的元素比较麻烦
`/**
 * 掌握:
 * 对象数组存储引用数据类型,也就是存的都是地址值
 * @author gyf
 *
 */
public class Demo01 {

    public static void main(String[] args) {
        //需求:我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
    
        //1.创建5个学生对象
        Student stu1 = new Student("马云", 50);
        Student stu2 = new Student("马化腾", 49);
        Student stu3 = new Student("李彦宏", 39);
        Student stu4 = new Student("刘强东", 49);
        Student stu5 = new Student("丁磊", 69);
        Student stu6 = new Student("小王", 69);
        
        //2.把学生对象存数组
        //int[] arr = new int[5];//基本数据类型的数组
        Student[] stus = new Student[6];//对象数组
        stus[0] = stu1;
        stus[1] = stu2;
        stus[2] = stu3;
        stus[3] = stu4;
        stus[4] = stu5;
        stus[5] = stu6;
        
        //3.遍历数组
        for(int i = 0; i < stus.length ;i++){
            //取元素
            Student  stu = stus[i];
            //4.打印学生信息
            System.out.println(stu);
        }
        
        
    }
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40
*   41

2.集合的继承体系

Collection:接口

  • List:接口
    –ArrayList【数组实现】
    –LinkedList 【链表实现】
    –Vector 【数组实现】
  • Set:接口
    –HashSet 【哈希】
    –TreeSet 【二叉树】

3.Collection的方法

  • 基本功能
boolean add(E e) 添加元素
boolean remove(Object o) 删除元素
void clear() 清除元素
boolean contains(Object o) 包含某一个元素
boolean isEmpty() 判断集合是否为空
int size() 获取集合的大小
  • 带All的功能测试
boolean addAll(Collection c) 把集合2添加到集合
boolean removeAll(Collection c) 把集合2中所有元素从集合1中删除
boolean containsAll(Collection c) 判断集合2中所有的元素在集合1中是否都有
boolean retainAll(Collection c) 取两个集合的交集

4.集合遍历

第一种:把集合转成数组(集合有个方法toArray),再进行遍历
第二种:使用集合的迭代器(集合有个方法iterator)进行遍历

评论

暂无评论