楔子:List 集合存储元素特点:1.有序性(元素有下标):以0开始,以1递增 .... 2.可重复性:可以储存重复的对象(根据 equals 方法判断是否为同一对象 )........

楔子:

List 集合存储元素特点:

1.有序性(元素有下标):以0开始,以1递增;

2.可重复性:可以储存重复的对象(根据 equals 方法判断是否为同一对象 ); 

List 常用方法:

由于 List 继承了 Collection ,所以 List 接口既有与 Collection 中的方法完全相同的方法,也肯定拥有自己特有的方法;

特有方法:

1.void add(int index,Object element) // 在集合的指定位置插入元素

list.add(1,"木");

2.Object  set(int index,Object  element) // 修改指定下标对应的元素

list.set(3,"火");

3.Object  get(int index) //返回指定下标对应的元素

Object obj=list.get(i);

4.int indexOf(Object o) //返回list集合中第一次出现o对象的索引位置,如果list集合中没有o对象,那么就返回-1

System.out.println(list.indexOf("金"));

5.int lastIndexOf(Object o) // 返回对象o在集合中最后一次出现处的索引,如果集合中没有o,则返回 -1。

System.out.println(list.lastIndexOf("火"));

6.Object  remove(int index) //删除指定下标对应的元素

list.remove(3);

 栗子老师:

import java.util.*;
public class pra {
    public static void main(String[] args) {
        List list=new LinkedList();
        list.add("金");
        list.add(1,"木");
        list.add(2,"水");
        list.add(3,"超人强");
        list.add(4,"小呆呆");
        //集合中有“金”元素,返回索引位置 0
        System.out.println(list.indexOf("金"));
        //集合中没有“火”元素,返回 -1
        System.out.println(list.lastIndexOf("火"));
        print(list);
        //删除集合中下标 3 对应的元素“超人强”,因此下标 4 对应的元素“小呆呆”位置前移一位
        list.remove(3);
        //修改集合中 3 下标对应元素“小呆呆”,将其改为 “火”
        list.set(3,"火");
        print(list);
    }
    //静态方法 遍历 List 集合元素
    public static void print(List list){
        for (int i=0;i<list.size();i++){
            Object obj=list.get(i);
            System.out.println(obj);
        }
    }
}

运行结果:
------------------------------
0
-1
金
木
水
超人强
小呆呆
金
木
水
火

Process finished with exit code 0

 foreach :

1.概念:

foreach 属于 for 循环的改版,用于遍历 数组 / 集合 尤为方便;

2.语法:

for ( 元素的类型  每一个元素的统一变量名 : 数组 / 集合 )   {   遍历数组 / 集合 中的元素  } 

3.注意:

foreach 循环中的元素类型必须每一个元素都满足,比如集合中既有 Double ,又有 String 类型对象,那这样元素类型就必须写 Object 才能遍历集合元素;

foreach 一般只用于遍历,因为在循环体中对元素进行赋值一般都会失败;

foreach 由于不存在下标的特点,在遍历方面,相较于for循环写代码十分方便;但若需要对指定下标元素进行操作就只能用 for 循环了;

4.栗子老师:

import java.util.*;
public class pra {
    public static void main(String[] args) {
        String[] strings = new String[3];
        //string 可以代表 strings 中的每一个元素,在此循环体中虽然对元素进行了赋值,但从输出结果可以看出并没有赋值成功
        for (String string : strings) {
            string = "giao";
            System.out.println(string);
        }
        //看一看上一个 foreach 是否对 strings 中的元素赋值成功
        for (String string : strings) {
            System.out.println(string);
        }
        List list = new LinkedList();
        list.add("王狗蛋");
        list.add(666);
        //由于Object为java中的祖宗类,所以在foreach循环中,将元素类型写成 Object 永远不会出错
        for (Object i : list) {
            System.out.println(i);
        }
    }
}

运行结果:
--------------------------------
giao
giao
giao
null
null
null
王狗蛋
666

Process finished with exit code 0

 泛型:

1.特性:

1)限定集合中的元素类型统一;

2)结合多态机制使用也很 nice ;

3)大多数业务中,集合中的元素类型都是统一的,所以这种泛型特性被大家所认可;

2..栗子老师:

package com.bjpowernode.javase.day2;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class pra {
    public static void main(String[] args) {
        List<String> list=new LinkedList<>();
        list.add("list 集合中只能添加 String 类型的元素哟 ~ ~ ");
        System.out.println(list.get(0));
        //集合中只能添加动物对象或动物子类对象
        List<Animal> listAnimal=new LinkedList<>();
        listAnimal.add(new Animal());
        listAnimal.add(new Dog());
        listAnimal.add(new Cat());
        //迭代器只能迭代动物对象或动物子类对象
        Iterator<Animal> iterator=listAnimal.iterator();
        while (iterator.hasNext()){
            //拿到元素
            Animal animal=iterator.next();
            //使现在迭代器指向的对象”移动“
            animal.move();
            //若迭代器现在指向的对象为 Cat 或 Cat 的子类,则调用 cat 的 buYu 方法
            if (animal instanceof Cat){
                Cat cat=(Cat)animal;
                cat.buYU();
            }
            //若迭代器现在指向的对象为 Dog 或 Dog 的子类,则调用 dog 的 chiBaBa 方法
            if (animal instanceof Dog){
                Dog dog=(Dog)animal;
                dog.chiBaBa();
            }
        }
    }
}
class Animal{
    public Animal() {
    }
    public void move(){
        System.out.println("动物在移动");
    }
}
class Dog extends Animal{
    public Dog() {
    }
    public void move(){
        System.out.println("小狗狗在移动");
    }
    public void chiBaBa(){
        System.out.println("狗狗在吃粑粑");
    }
}
class Cat extends Animal{
    public Cat() {
    }
    public void move(){
        System.out.println("小猫猫在移动");
    }
    public void buYU(){
        System.out.println("猫猫在捕鱼");
    }
}

运行结果:
-----------------------------
list 集合中只能添加 String 类型的元素哟 ~ ~ 
动物在移动
小狗狗在移动
狗狗在吃粑粑
小猫猫在移动
猫猫在捕鱼

Process finished with exit code 0