2.List:
特有方法。凡是可以操作角标的方法都是该体系特有的方法。
增
add(index,element);
addAll(index,Collection);
删
remove(index);
改
set(index,element);
查
get(index):
subList(from,to);
listIterator();
int indexOf(obj):获取指定元素的位置。
ListIterator listIterator();
List集合特有的迭代器。ListIterator是Iterator的子接口。
在迭代时,不可以通过集合对象的方法操作集合中的元素。
因为会发生ConcurrentModificationException异常。
所以,在迭代器时,只能用迭代器的放过操作元素,可是Iterator方法是有限的,
只能对元素进行判断,取出,删除的操作,
如果想要其他的操作如添加,修改等,就需要使用其子接口,ListIterator。
该接口只能通过List集合的listIterator方法获取。
import java.util.*;
class ArrayListTest {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
a1.add("java01");
a1.add("java01");
a1.add("java013");
a1.add("java014");
a1.add("java014");
a1.add("java0145");
sop(a1);
sop(Test.myRemove(a1,a2));
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
class Test{
public static ArrayList myRemove(ArrayList a,ArrayList b) {
// 新建一个List然后进行判断,如果有就不要存,这样就保证的元素的唯一性
if(a.isEmpty()){
return a;
}
ListIterator it = a.listIterator();
while(it.hasNext()){
Object obj = it.next();
if(!(b.contains(obj))){
b.add(obj);
}
}
return b;
}
}
练习二:
/*
要求:去除ArrayList 中的相同对象
注意:要重写equals()方法。
*/
import java.util.*;
class Person{
private int age;
private String name;
Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
return false;
}
Person p = (Person) obj;
// 要向下转型
// System.out.println("lala");
if(this.name.equals(p.name)&&this.age==p.age){
return true;
}else {
return false;
}
}
}
class Test{
public static ArrayList myRemove(ArrayList a,ArrayList b) {
if(a.isEmpty()){
return a;
}
ListIterator it = a.listIterator();
while(it.hasNext()){
Object obj = it.next();
Person p = (Person) obj;
if(!(b.contains(p))){
b.add(p);
}
}
return b;
}
}
public class ArrayListTest2{
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
a1.add(new Person("java01",12));
a1.add(new Person("java01",12));
a1.add(new Person("java013",23));
a1.add(new Person("java014",33));
a1.add(new Person("java014",33));
a1.add(new Person("java0145",44));
sop(a1);
sop(Test.myRemove(a1,a2));
}
public static void sop(ArrayList a){
ListIterator l = a.listIterator();
while(l.hasNext()){
Object obj =l.next();
if (obj instanceof Person) {
Person p = (Person) obj;
System.out.println(p.getName()+"...."+p.getAge());
}else {
break;
}
}
}
}
3.Set
|--Set:元素是无序(存入和取出的顺序不一定一致),元素不可以重复。、
|--HashSet:底层数据结构是哈希表。是线程不安全的。不同步。
HashSet是如何保证元素唯一性的呢?
是通过元素的两个方法,hashCode和equals来完成。
如果元素的HashCode值相同,才会判断equals是否为true。
如果元素的hashcode值不同,不会调用equals。
注意,对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashcode和equals方法。
|--TreeSet:
Set集合的功能和Collection是一致的。
import java.util.*;
class Person{
private int age;
private String name;
Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public int hashCode(){
return this.name.hashCode()+this.age;
}
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
return false;
}
Person p = (Person) obj;
// System.out.println("lala");
if(this.name.equals(p.name)&&this.age==p.age){
return true;
}else {
return false;
}
}
}
public class HashSetDemo{
public static void main(String[] args) {
HashSet a1 = new HashSet();
HashSet a2 = new HashSet();
System.out.println(a1.add(new Person("java01",12)));
a1.add(new Person("java01",12));
a1.add(new Person("java013",23));
a1.add(new Person("java014",33));
System.out.println(a1.add(new Person("java014",33)));
a1.add(new Person("java0145",44));
sop(a1);
// sop(Test.myRemove(a1,a2));
}
public static void sop(HashSet a){
Iterator l = a.iterator();
while(l.hasNext()){
Object obj =l.next();
if (obj instanceof Person) {
Person p = (Person) obj;
System.out.println(p.getName()+"...."+p.getAge());
}else {
break;
}
}
}
}
import java.util.*;
class Student implements Comparable {
String name;
int age;
Student(String name, int age ){
this.name = name;
this.age = age;
}
public int compareTo(Object obj){
if(!(obj instanceof Student)){
throw new RuntimeException("不是学生对象");
}
Student s = (Student)obj;
if(this.age > s.age){
return 1;
}else if (this.age < s.age) {
return -1;
}else{
return this.name.compareTo(s.name);
}
}
}
public class TreeSetDemo{
public static void main(String[] args) {
TreeSet t = new TreeSet();
t.add(new Student("nuddles1",22));
t.add(new Student("nuddles1",25));
t.add(new Student("nuddles1",22));
t.add(new Student("nuddles2",22));
t.add(new Student("nuddles1",24));
t.add(new Student("nuddles1",15));
Iterator it = t.iterator();
while (it.hasNext()) {
Student stu = (Student) it.next();
sop(stu.name+"...."+stu.age);
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
import java.util.*;
class Student implements Comparable {
String name;
int age;
Student(String name, int age ){
this.name = name;
this.age = age;
}
public int compareTo(Object obj){
if(!(obj instanceof Student)){
throw new RuntimeException("不是学生对象");
}
Student s = (Student)obj;
if(this.age > s.age){
return 1;
}else if (this.age < s.age) {
return -1;
}else{
return this.name.compareTo(s.name);
}
}
}
class Mycompare implements Comparator{
public int compare(Object a, Object b){
if(!(a instanceof Student && b instanceof Student)){
throw new RuntimeException("不是学生对象");
}
Student s1 = (Student)a;
Student s2 = (Student)b;
if(s1.name.equals(s2.name)){
return Integer.compare(s1.age,s2.age);
}else {
return s1.name.compareTo(s2.name);
}
}
}
public class TreeSetDemo{
public static void main(String[] args) {
TreeSet t = new TreeSet(new Mycompare());
t.add(new Student("nuddles1",22));
t.add(new Student("nuddles1",25));
t.add(new Student("nuddles1",22));
t.add(new Student("nuddles2",22));
t.add(new Student("nuddles1",24));
t.add(new Student("nuddles1",15));
Iterator it = t.iterator();
while (it.hasNext()) {
Student stu = (Student) it.next();
sop(stu.name+"...."+stu.age);
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
class Test{
public<T> void show(T t){
System.out.println(t);
}
public<T> void run(T t){
System.out.println(t.hashCode());
//使用的方法必须是所有类型都能使用的
}
}
class GenericDemo {
public static void main(String[] args) {
// Test<String> t = new Test<String>();
Test t = new Test();
t.show("8");
t.run("2");
}
}
泛型定义在接口上
interface Test<T> {
public abstract void show(T t);
}
class Person<T> implements Test<T>{
public void show(T t){
System.out.println(t);
}
}
public class genericDemo2{
public static void main(String[] args) {
new Person<Integer>().show(5);
}
}
import java.util.*;
class Person {
private String name;
Person(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
class Student extends Person{
Student(String name){
super(name);
}
}
class Worker extends Person{
Worker(String name){
super(name);
}
}
class Mycompare implements Comparator<Person>{
public int compare(Person p1,Person p2){
return p1.getName().compareTo(p2.getName());
}
}
public class GenericDemo3 {
public static void main(String[] args) {
TreeSet<Person> t = new TreeSet<Person>(new Mycompare());
t.add(new Student("nuddles01"));
t.add(new Student("nuddles02"));
t.add(new Student("nuddles03"));
t.add(new Worker("nuddles--01"));
t.add(new Worker("nuddles--02"));
t.add(new Worker("nuddles--05"));
t.add(new Worker("nuddles--03"));
Iterator<Person> it = t.iterator();
while(it.hasNext()){
sop(it.next().getName);
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}