第八章集合操作 急 1.对数组对象的操作(Arrays), 2.对象集合(Set) 3.对象列表(儿ist) 4.对象映射(Map) 5.对对象数组的操作(Collections) 6.枚举(Enumeration)和送代(Iterator) 7.小结
1 1. 对数组对象的操作(Arrays) 2. 对象集合(Set) 3. 对象列表(List) 4. 对象映射(Map) 5. 对对象数组的操作(Collections) 6. 枚举(Enumeration)和迭代(Iterator) 7. 小结 第八章 集合操作
对数组对象的操作(Arrays) 急 ■java.util.Arrays This class contains various methods for manipulating arrays ■排序(sorting). ■ 搜索(searching) ■ 填充(filling) 2
2 ◼ java.util.Arrays类 ◼ This class contains various methods for manipulating arrays ◼ 排序(sorting) ◼ 搜索(searching) ◼ 填充(filling) 对数组对象的操作(Arrays)
boolean[],char[],byte[],short[],int[],long[],double[],float[] Object[] 全部是静态方法 ■ public static int binarySearch(byte[]a,byte key) Searches the specified array of bytes for the specified value using the binary search algorithm. public static boolean equals(byte[]a,byte[]a2) Returns true if the two specified arrays of bytes are equal to one another. ◆ public static void fill(byte[]a,byte val) Assigns the specified byte value to each element of the specified array of bytes. public static void fill(byte[]a,int fromIndex,int toIndex, byte val) Assigns the specified byte value to each element of the specified range of the specified array of bytes. public static void sort(byte[]a) Sorts the specified array of bytes into ascending numerical order. 3
3 ◼ 全部是静态方法 ◼ public static int binarySearch(byte[] a, byte key) Searches the specified array of bytes for the specified value using the binary search algorithm. ◼ public static boolean equals(byte[] a, byte[] a2) Returns true if the two specified arrays of bytes are equal to one another. ◼ public static void fill(byte[] a, byte val) Assigns the specified byte value to each element of the specified array of bytes. ◼ public static void fill(byte[] a, int fromIndex, int toIndex, byte val) Assigns the specified byte value to each element of the specified range of the specified array of bytes. ◼ public static void sort(byte[] a) Sorts the specified array of bytes into ascending numerical order. 对数组对象的操作(Arrays) boolean[], char[], byte[], short[], int[], long[], double[], float[] Object[]
对教组对象的操作(Arrays) import java.util.Arrays; public class ArrayDemo1 public static void main(String args[]) int[]a1 new int[10] int[]a2 new int[10]; Arrays.fill(a1,47); Arrays.fill(a2,47); System.out.printIn(Arrays.equals(a1,a2); a2[3]=11;a2[2]=9; System.out.println(Arrays.equals(a1,a2)); Arrays.sort(a2); System.out.println(Arrays.binarySearch(a2,11)); 4
4 对数组对象的操作(Arrays) import java.util.Arrays; public class ArrayDemo1 { public static void main(String args[]) { int[] a1 = new int[10]; int[] a2 = new int[10]; Arrays.fill(a1, 47); Arrays.fill(a2, 47); System.out.println(Arrays.equals(a1, a2)); a2[3]=11; a2[2]=9; System.out.println(Arrays.equals(a1, a2)); Arrays.sort(a2); System.out.println(Arrays.binarySearch(a2, 11)); } }
第八章集合操作 急 1.对数组对象的操作(Arrays). 2.对象集合(Set) 3.对象到表(List) 4.对象映射(Map) 5.对对象数组的操作(Collections) 6.枚举(Enumeration)和送代(Iterator) 7.小结
5 1. 对数组对象的操作(Arrays) 2. 对象集合(Set) 3. 对象列表(List) 4. 对象映射(Map) 5. 对对象数组的操作(Collections) 6. 枚举(Enumeration)和迭代(Iterator) 7. 小结 第八章 集合操作
对象集合(Set) 盖 ■数学上“集合”瓶念的抽象,无序 A Set is a collection that cannot contain duplicate elements. ■ 集合与元素 集合之间 ■ 无序集合和有序集合 6
6 ◼ 数学上“集合”概念的抽象,无序 ◼ A Set is a collection that cannot contain duplicate elements. ◼ 集合与元素 ◼ 集合之间 ◼ 无序集合和有序集合 对象集合(Set)
对象集合(Set) 急 java.util.Set接口(集合与元素) public boolean add(Object o) Adds the specified element to this set if it is not already present. ■ public boolean remove(Object o) Removes the specified element from this set if it is present. public boolean contains(Object o) Returns true if this set contains the specified element. ■ public int size( Returns the number of elements in this set. 7
7 ◼ java.util.Set 接口 (集合与元素) ◼ public boolean add(Object o) Adds the specified element to this set if it is not already present. ◼ public boolean remove(Object o) Removes the specified element from this set if it is present. ◼ public boolean contains(Object o) Returns true if this set contains the specified element. ◼ public int size() Returns the number of elements in this set. 对象集合(Set)
D:\java FindDups1 i came i saw i left 对象集合(Set Duplicate:i Duplicate:i java.util.Hash$4 distinct words:[came,left,saw,il ■无序集合 D import java.util.*; public class SetDemo1 public static void main(String args[]){ Set s new HashSet(); for (int i=0;i<args.length;i++) if (!s.add(args[i])) System.out.println("Duplicate:"+args[i]); System.out.println(s.size()+"distinct words:"+s); 8
8 ◼ java.util.HashSet implement java.util.Set ◼ 无序集合 对象集合(Set) import java.util.*; public class SetDemo1 { public static void main(String args[]) { Set s = new HashSet(); for (int i=0; i<args.length; i++) if (!s.add(args[i])) System.out.println("Duplicate: "+args[i]); System.out.println(s.size()+" distinct words: "+s); } } D:\java FindDups1 i came i saw i left Duplicate: i Duplicate: i 4 distinct words: [came, left, saw, i] D:\
对象集合(Set) 急 java.util.Set接口(集合之间) ■ public boolean containsAll(Collection c) Returns true if this set contains all of the elements of the specified collection. public boolean addAll(Collection c) Adds all of the elements in the specified collection to this set if they're not already present. ■ public boolean removeAll(Collection c) Removes from this set all of its elements that are contained in the specified collection. public boolean retainAll(Collection c) Retains only the elements in this set that are contained in the specified collection. 9
9 ◼ java.util.Set接口 (集合之间) ◼ public boolean containsAll(Collection c) Returns true if this set contains all of the elements of the specified collection. ◼ public boolean addAll(Collection c) Adds all of the elements in the specified collection to this set if they're not already present. ◼ public boolean removeAll(Collection c) Removes from this set all of its elements that are contained in the specified collection. ◼ public boolean retainAll(Collection c) Retains only the elements in this set that are contained in the specified collection. 对象集合(Set)
D:\java FindDups2 i came i saw i left 对象集合(Set i Unique words:[came,left,saw] Duplicate words:[i] java.util.Hashs import java.util.*; D public class SetDemo2 public static void main(String args[]){ Set uniques new HashSet(); Set dups new HashSet(); for (int i=0;i<args.length;i++) if (!uniques.add(args[i])) dups.add(args[i]); uniques.removeAll(dups); System.out.println("Unique words:"uniques); System.out.printin("Duplicate words:"dups); 10
10 ◼ java.util.HashSet implement java.util.Set 对象集合(Set) import java.util.*; public class SetDemo2 { public static void main(String args[]) { Set uniques = new HashSet(); Set dups = new HashSet(); for (int i=0; i<args.length; i++) if (!uniques.add(args[i])) dups.add(args[i]); uniques.removeAll(dups); System.out.println("Unique words: " + uniques); System.out.println("Duplicate words: " + dups); } } D:\java FindDups2 i came i saw i left Unique words: [came, left, saw] Duplicate words: [i] D:\