请编写程序,将自定义对象存储到ArrayList集合,使用泛型并遍历
a) 使用迭代器遍历
b) 使用列表迭代器遍历
c) 使用size()和get()方法遍历
d) 使用增强for遍历
package org.westos.demo2;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Demo {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("张三", 23));
list.add(new Student("李四", 22));
list.add(new Student("王五", 25));
Iterator<Student> iterator = list.iterator();
System.out.println("迭代器进行遍历");
while (iterator.hasNext()) {
Student next = iterator.next();
System.out.println(next);
}
System.out.println("列表迭代器进行遍历");
ListIterator<Student> studentListIterator = list.listIterator();
while (studentListIterator.hasNext()) {
Student next = studentListIterator.next();
System.out.println(next);
}
System.out.println("size()和get()方法进行遍历");
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
System.out.println("使用增强for()循环进行遍历");
for (Student student : list) {
System.out.println(student);
}
}
}
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Vector的概述及使用方法
Vector类概述: Vector 类可以实现可增长的对象数组 , Vector 是同步的。
Vector类特有功能: public void addElement(E obj) 给Vector集合添加元素 public E elementAt(int index) 查找某一元素的索引 public boolean removeElement(Object obj) 删除集合中的某个元素 public void removeElementAt(int index) 删除集合中指定索引处的元素 public void setElementAt(E obj, int index) 用指定元素替换指定索引处的元素 public Enumeration elements() 创建一个迭代器
案例演示
请编写程序,将自定义对象存储到Vector集合,使用泛型并遍历
a) 使用迭代器遍历
b) 使用Vector特有的迭代器遍历
c) 使用size()和get()方法遍历
d) 使用增强for遍历
package org.westos.demo2;
import java.util.*;
public class Demo {
public static void main(String[] args) {
Vector<Student> list = new Vector<>();
list.add(new Student("张三", 23));
list.add(new Student("李四", 22));
list.add(new Student("王五", 25));
Iterator<Student> iterator = list.iterator();
System.out.println("迭代器进行遍历");
while (iterator.hasNext()) {
Student next = iterator.next();
System.out.println(next);
}
System.out.println("Vector特有的迭代器进行遍历");
Enumeration<Student> elements = list.elements();
while (elements.hasMoreElements()) {
Student next = elements.nextElement();
System.out.println(next);
}
System.out.println("size()和get()方法进行遍历");
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
System.out.println("使用增强for()循环进行遍历");
for (Student student : list) {
System.out.println(student);
}
}
}
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
LinkedList的概述及使用方法
LinkedList类概述: List 接口的链接列表实现 , 此实现不是同步的
LinkedList类特有功能: public void addFirst(E e) 给集合最顶层添加元素 public void addLast(E e) 给集合的末尾添加元素 public E getFirst() 获取集合的首位元素 public E getLast() 获取集合的末尾元素 public E removeFirst() 删除集合的首位元素 public E removeLast() 删除集合的末尾元素
案例演示:
请编写程序,将自定义对象存储到LinkedList集合,使用泛型并遍历
a) 使用迭代器遍历
b) 使用列表迭代器遍历
c) 使用size()和get()方法遍历
d) 使用增强for遍历
package org.westos.demo2;
import java.util.*;
public class Demo {
public static void main(String[] args) {
LinkedList<Student> list = new LinkedList<>();
list.add(new Student("张三", 23));
list.add(new Student("李四", 22));
list.add(new Student("王五", 25));
Iterator<Student> iterator = list.iterator();
System.out.println("迭代器进行遍历");
while (iterator.hasNext()) {
Student next = iterator.next();
System.out.println(next);
}
System.out.println("列表迭代器进行遍历");
ListIterator<Student> studentListIterator = list.listIterator();
while (studentListIterator.hasNext()) {
Student next = studentListIterator.next();
System.out.println(next);
}
System.out.println("size()和get()方法进行遍历");
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
System.out.println("使用增强for()循环进行遍历");
for (Student student : list) {
System.out.println(student);
}
}
}
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Demo {
public <T> T show(T a) {
System.out.println(a);
return a;
}
}
泛型接口的演示
package org.westos.demo4;
import java.util.Collection;
public interface MyInterface<T,U,R> { //泛型接口
public R show(T t,U u);
}
//泛型接口上的泛型,什么时候名曲,你可以在子类里面去明确
class MyDemo implements MyInterface<String,Integer,Integer>{
@Override
public Integer show(String s, Integer integer) {
return null;
}
}
泛型高级之通配符
泛型高级的表达方式: (1):泛型通配符<?>: 任意类型,如果没有明确,那么就是* Object以及任意的Java类了 (2): extends E : 泛型的向下限定,E及其子类 (3): super E : 泛型的向上限定,E及其父类
案例演示:
class Animal {}
class Dog extends Animal{}
class Cat extends Animal{}
class GenericDemo {
public static void main(String[] args) {
Collection<Object> c = new ArrayList<Object>();
Collection<Animal> c1 = new ArrayList<Animal>();
Collection<? extends Animal> c5 = new ArrayList<Animal>();
Collection<? extends Animal> c6 = new ArrayList<Dog>();
Collection<? extends Animal> c7 = new ArrayList<Cat>();
Collection<? super Animal> c8 = new ArrayList<Object>();
Collection<? super Animal> c9 = new ArrayList<Animal>();
}
}
请编写程序,完成N个数 相加的功能,并测试
package org.westos.demo2;
public class Demo2 {
public static void main(String[] args) {
Integer sum = sum(1, 2, 3,4);
System.out.println(sum);
}
public static Integer sum(Integer ...a){
int sum=0;
for (int i = 0; i < a.length; i++) {
sum+=a[i];
}
return sum;
}
}