1 ArrayList
(1)ArrayList 存儲字符串並遍歷
ArraList aray =new ArrayList();
//創建元素對象,並添加元素
array.add("hello");
array.add("world");
array.add("java");
//遍歷
Iterator it = array.iterator();
while(it.hasNext()){
String s = (String)it.next();
System.out.println(s);
}
(2) ArrayList存儲自定義對象並遍歷
//創建集合對象
ArrayList array = new ArrrayList();
//創建學生對象
Student s1 = new Student("武松",30);
Student s2 = new Student("林沖",40);
Student s3 = new Student("魯智深",35);
Student s4 = new Student("楊志",32);
//添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//遍歷
Iterator it = array.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
2 Vector 的特有功能
A:添加功能 被這些替代(簡化書寫)
public void addElement(Object obj) --- add()
B:獲取功能
public Object elementAt(int index) --- get()
public Enumeration elements() --- Iterator iterator()
boolean hasMoreElements() hasNext()
Object nextElements next()
jdk升級的原因:
A:安全 B:效率 D:簡化書寫
--------------------------------------------------------
// 創建集合對象
Vector v = new Vector();
// 添加功能
v.addElement("hello");
v.addElement("world");
v.addElement("java");
//遍歷
for(int x = 0;x < v.size();x++){
String s = (String)v.elementAt(x);
System.out.println(s);
}
Enumeratiron en = v.elements();//返回的是實現類的對象
while(en.hasMoreElements()){
String s =(String)en.nextElement();
System.out.println(s);
}
//輸出 hello
world
java
hello
world
java
3 LinkedList 的特有功能
A:添加功能
public void addFirst(Object e)
public void addLast(Object e)
B:獲取功能
public Object getFirst()
public Object getLast()
C:刪除功能
public Object removeFirst() //刪除並返回
public Object removeLast()
---------------------------------------
LinkedList link =new LinkedList();
//創建元素對象,並添加元素
link.add("hello");
link.add("world");
link.add("java");
link.addFirst("javaee");
System.out.println("link:"+link);
//輸出link:[javaee,hello,world,java]
4 練習
(1):ArrayList 去除集合中字符串的重複值
//創建集合對象
ArrayList array = new ArrrayList();
//添加多個字符串元素
array.add("hello");
array.add("world");
array.add("java");
array.add("hello");
array.add("world");
array.add("java");
array.add("hello");
array.add("world");
array.add("java");
//創建新集合
ArrayList newArray = new ArrrayList();
//遍歷舊數組,獲取得到每一個元素
Iterator it = array.iterator();
while(it.hasNext()){
String s = (String)it.next();
//拿這個元素到新集合去找,看有沒有
if(!newArray.contains(s)){
newArray.add(s);
}
}
//遍歷新集合
for(int x = 0;x<newArray.size();x++){
String s = (String) newArray.get(x);
System.out.println(s);
}
(2)ArrayList 去除集合中字符串的重複值---但不能創建新的集合,就在以前的集合上做
//創建集合對象
ArrayList array = new ArrrayList();
//添加多個字符串元素
array.add("hello");
array.add("world");
array.add("java");
array.add("hello");
array.add("world");
array.add("java");
array.add("hello");
array.add("world");
array.add("java");
//用選擇排序思想,那前面的和後面的相比較,如果有,就刪除
for(int x = 0;x<array.size()-1:x++){
for(int y = x+1;y<array.size();y++){
if(array.get(x).equals(array.get(y))){
array.remove(y);
}
}
}
Iterator it = array.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s);
}
(3)ArrayList 去除集合中自定義對象的重複值(對象的成員變量值都相同)
//創建集合對象
ArrayList array = new ArrrayList();
//創建學生對象
Student s1 = new Student("武松",30);
Student s2 = new Student("林沖",40);
Student s3 = new Student("魯智深",35);
Student s4 = new Student("楊志",32);
Student s5 = new Student("楊志",32);
//添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
array.add(s5);
//創建新集合
ArrayList newArray = new ArrrayList();
//遍歷舊數組,獲取得到每一個元素
Iterator it = array.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
//拿這個元素到新集合去找,看有沒有
if(!newArray.contains(s)){
newArray.add(s);
}
}
//遍歷新集合
for(int x = 0;x<newArray.size();x++){
String s = (String) newArray.get(x);
System.out.println(s);
}
-----------------------------------------------
但是這樣會出問題,通過簡單分析,知道了問題出現在判斷語句上
contains()方法的底層依賴的是equals()方法
而自定義的學生類中沒有equals()方法,這個時候,默認使用的是他父類Object的equals()方法
Object類的equals()默認比較的地址值,所以,它們都進去了,因爲new的東西,地址值不同
所以,比較成員變量的值,重寫equals ,用自動生成就行了
-----------------------------------------------
***(4) 用LinkedList模擬棧數據結構的集合,並測試
題目的意思是:
自定定義一個結合類,在這個集合類內部可以使用LinkedList模擬
A:linkedList的特點添加功能addFirst()
B: 棧的特點先進後出
public class MyStack{
private LinkedList link;
public MyStack(){
link = new LinkedList();
}
public void add(Object obj){
link.addFirst(obj);
}
public Object get(){
// return link.getFirst();
return link.removeFirst();
}
//判斷是否爲空
public boolean isEmpty(){
return link.isEmpty();
}
}
//測試
MyStack ms = new MyStack();
//添加元素
ms.add("hello");
ms.add("world");
ms.add("java");
while(!ms.isEmpty()){
System.out.println(ms.get());
}
//輸出 java
world
hello
5 泛型
泛型:是一種把類型明確的工作推遲到創建對象或者調用方法的時候纔去明確的特殊類型。參數化類型,把類型當做參數一樣的傳遞。
格式:
<數據類型>
***此處的數據類型只能是引用類型。8種基本數據類型不行,但可以用包裝類。
好處:
A:把運行時期的問題提前到了編譯期間
B:避免了強制類型轉換
C:優化了程序設計,解決了黃色警告線
泛型在哪些地方使用呢?看API,如果類,接口,抽象類後面跟着有<E>就說明要使用泛型,一般來說在集合中使用
--------------------------------------------------
ArraList<String> aray =new ArrayList<String>();
//創建元素對象,並添加元素
array.add("hello");
array.add("world");
array.add("java");
//遍歷
Iterator<String> it = array.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
---------------------------------------------------
//創建集合對象
ArrayList<Student> array = new ArrrayList<Student>();
//創建學生對象
Student s1 = new Student("武松",30);
Student s2 = new Student("林沖",40);
Student s3 = new Student("魯智深",35);
Student s4 = new Student("楊志",32);
//添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//遍歷
Iterator<Student> it = array.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
6 泛型
早期的時候,我們使用Object來代表任意的類型。
向上轉型是沒有任何問題的,但是在向下轉型的時候其實隱藏了類型轉換的問題。
也就是說這樣的程序其實並不是安全的,所以java在jdk5後引入了泛型,提高了程序的安全性
泛型類
把泛型定義在類上
格式:public class 類名<泛型類型1,..>
注意 泛型類型必須是引用類型
泛型方法
把泛型定義在方法上
格式:public<泛型類型>返回類型 方法名(泛型類型)
泛型接口
把泛型定義在接口上
格式:public interface 接口名<泛型類型1,...>
(1)泛型類
public class ObjectTool<T>{
private T obj;
public T getObj(){
return obj;
}
public void setObj(){
this.obj = obj;
}
-------------------------------------------
//測試
ObjectTool ot = new ObjectTool();
ot.setObj(new String("風清揚"));
String s = (String)ot.getObj();
System.out.println(s);
//風清揚
ot.setObj(new Integer(30));
Integer i = (Integer)ot.getObj();
System.out.println(i);
//30
ot.setObj(new String("風清"));
Integer i = (Integer)ot.getObj();
System.out.println(s);
//報錯
--------------------------------------------
//應該這麼使用
ObjectTool<String> ot = new ObjectTool<String>();
//ot.setObj(new Integer(30));//這個時候編譯期間就會過不去
ot.setObj(new String("風清揚"));
String s =ot.getObj(); //不用轉型
System.out.println(s);
ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
//ot.setObj(new String("風清揚"));//這個時候編譯期間就會過不去
ot.setObj(new Integer(30));
Integer i = ot2.getObj();
System.out.println(i);
(2)泛型方法
public class ObjectTool(
public <T> void show(T t){
System.out.println(t);
}
ObjectTool ot = new ObjectTool();
ot.show("hello");
ot.show(100);
ot.show(true);
//輸出:hello
100
true
(3)泛型接口
public interface inter<T>{
public abstract void show(T t);
}
//實現類在實現接口的時候
//第一種情況:已經知道是什麼類型的了
//第二種情況:還不知道是什麼類型的
//第一種情況測試
public class InterImpl implements Inter<String>{
@Override
public void show(String t){
System.out.println(t);
}
}
Inter<String> i = new InterImpl();i.show("hello");
//第二種情況測試
public class InterImpl<T> implements Inter<T>{
@Override
public void show(T t){
System.out.println(t);
}
Inter<String> i = new InterIml<String>();
i.show("hello");
Inter<Integer> i = new InterIml<Integer>();
i.show(100);
//輸出: hello 100
//但又回到泛型類當中了。。。。
7 泛型高級(通配符)
(1)?:任意類型,如果沒有明確,那麼就是Object以及任意的java類了
//泛型如果明確的寫的時候,前後必須一致
Collection<Object> c1 = new ArrayList<Object>();
//Collection<Object> c2 = new ArrayList<Animal>(); //報錯
//Collection<Object> c3 = new ArrayList<Dog>(); //報錯
//Collection<Object> c4 = new ArrayList<Cat>(); //報錯
//?表示任意裏的類型都是可以的
Collection<?> c5 = new ArrayList<Object>();
Collection<?> c6 = new ArrayList<Animal>(); //不會報錯
Collection<?> c7 = new ArrayList<Dog>(); //不會報錯
Collection<?> c8 = new ArrayList<Cat>(); //不會報錯
(2)? extends E:向下限定,E及其子類
Collection<? extends Animal> c9 = new ArrayList<Object>(); //報錯
Collection<? extends Animal> c10 = new ArrayList<Animal>();//不會報錯
Collection<? extends Animal> c11 = new ArrayList<Dog>(); //不會報錯
Collection<? extends Animal> c12 = new ArrayList<Cat>(); //不會報錯
? super E: 向上限定 ,E及其父類
Collection<? super Animal> c9 = new ArrayList<Object>(); //不會報錯
Collection<? super Animal> c10 = new ArrayList<Animal>();//不會報錯
Collection<? super Animal> c11 = new ArrayList<Dog>(); //報錯
Collection<? super Animal> c12 = new ArrayList<Cat>(); //報錯
8 JDK5新特性:自動拆裝箱,泛型,增加for,靜態導入,可變參數,枚舉
(1) 增加for:是for循環的一種
for(元素數據類型 變量:數組或者Collection集合){
使用變量即可,該變量就是元素
}
int[] arr = {1,2,3,4,5}
for(int x:arr){
System.out.println(x);
}
//輸出 1 2 3 4 5
好處: 簡化了數據和集合的遍歷
弊端: 增加for的目標不能爲null
----------------------------------------------
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
//遍歷集合
//迭代器
Iterator<String> it = array.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
//普通for
for(int x = 0;x<arry.size();x++){
String s = array.get(x);
Sytem.out.println(s);
}
//增加for
for(String s : array){
System.out.println(s);
}
// hello
world
java
---------------------------------------------------
//創建集合對象
ArrayList<Student> array = new ArrrayList<Student>();
//創建學生對象
Student s1 = new Student("武松",30);
Student s2 = new Student("林沖",40);
Student s3 = new Student("魯智深",35);
Student s4 = new Student("楊志",32);
//添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//遍歷
//迭代器
Iterator<Student> it = array.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
//普通for
for(int x =0;x<array.size();x++){
Student s =array.get(x);
System.out.println(s.getName()+"---"+s.getAge());
}
//增加for
for(Student s :array){
System.out.println(s.getName()+"---"+s.getAge());
}
(2)靜態導入
靜態導入:可以直接導入方法的級別
注意事項:
A:方法必須是靜態的
B:如果有多個同名的靜態方法,容易不知道使用誰,這個時候使用需要加別稱。
-----------------由此可見,意義不大,但要看得懂就行-------------
System.out.println(java.lang.Math.abs(-100));
System.out.println(java.lang.Math.pow(2,3)); //2的3次方8
//太複雜,我們就引入到import
import java.lang.MathSystem.out.println(Math.abs(-100));
System.out.println(Math.pow(2,3));
//太複雜,有更簡單的
import java.lang.Math.absimport java.lang.Math.pow
System.out.println(abs(-100));
System.out.println(pow(2,3));
(3)可變參數
可變參數:定義方法的時候不知道該定義多少個參數
格式:
修飾符 返回值類型 方法名(數據類型...變量名){
}
注意:這裏的變量其實是一個數組
int a = 30;
int b = 30;
result =sum (a,b);
System.out.println(result);
int c = 30;
result =sum (a,b,c);
System.out.println(result);
public static int sum(int...a) {
int s = 0;
for(int x : a){
s+=x;
}
return s;
}
//輸出: 60
90
9 Arrays 工具類的 asList() 方法的使用
public static <T> List<T> asList(T...a):把數組轉成集合
注意:雖然可以把數組轉成集合,但是集合的長度不能改變,其本質是數組。
//String[] strArray ={"hello","world","java"};
//List<String> list = Arrays.asList(strArray);
List<String> list = Arrays.asList("hello","world","java");
for(String s: list){
System.out.println(s);
}
//輸出:hello
world
java
//list.add("javaee");//報錯
//list.remove(1); //報錯
list.set(1,"javaee");//不報錯
10 練習
(1)集合的嵌套遍歷
需求:我們班有學生,每個學生是一個對象,所以我們可以使用一個集合表示我們班級的學生, ArrayList<Student>
但是還有其他的班級,所以其他班級也有一個ArrayList<Student>
於是:ArrayList<ArrayList<Student>>
//創建大集合
ArrayList<ArrayList<Student>> bigArrayList =new ArrayList<ArrayList<Student>>();
//創建第一個班級的學生集合
ArrayList<Student> firstArrayList = new ArrayList<Student>();
Student s1 = new Student("武松",30);
Student s2 = new Student("林沖",40);
Student s3 = new Student("魯智深",35);
//學生1進班
firstArrayList.add(s1);
firstArrayList.add(s2);
firstArrayList.add(s3);
//把第一個班級存儲到學生系統中
bigArrayList.add(firstArrayList);
//創建第二個班級的學生集合
ArrayList<Student> secondArrayList = new ArrayList<Student>();
Student s4 = new Student("賈寶玉",22);
Student s5 = new Student("林黛玉",21);
Student s6 = new Student("薛寶釵",20);
//學生2進班
secondArrayList.add(s4);
secondArrayList.add(s5);
secondArrayList.add(s6);
//把第二個班級存儲到學生系統中
bigArrayList.add(secondArrayList);
//遍歷集合
for(ArrayList<Student> arry: bigArrayList){
for(Student s : array){
System.out.println(s.getName()+"---"+s.getAge());
}
}
(2)產生10個1-20之間的隨機數,要求隨機數不能重複
用數組實現,但是數組的長度是固定的,長度不好確定
所以我們使用集合實現
//創建產生隨機數的對象
Random r = new Random();
//創建一個存儲隨機的集合
ArrayList<Integer> array = new ArrayList<Integer>();
//定義一個統計變量,從0開始
int count = 0;
//判斷統計遍歷是否小於10
while(count < 10){
//先產生一個隨機數
int number =r.nextInt(20)+1;
//判斷該隨機數在集合中是否存在
if(!array.contains(number)){
//如果不存在,就添加,統計++
array.add(number);
count++;
}
}
//遍歷集合
for(Integer i:array){
System.out.println(i);
}
(3)鍵盤錄入多個數據,以0結束,要求在控制檯輸出這多個數據中的最大值
//創建鍵盤錄入數據對象
Scanner sc = new Scanner(System.in);
//鍵盤錄入多個數據,我們不知道多少個,所以用集合儲存
ArrayList<Integer> array = new ArrayList<Integer>();
//以0結束,這個簡單,只要鍵盤錄入的數據是0,就不再錄入了
while(true){
System.out.println("請輸入:");
int number = sc. nextInt();
if(number!=0){
array.add(number);
} else{
break;
}
}
//把集合轉成數組
Integer[] i = new Integer[array.size()]
array.toArray(i);
//對數組排序
Arrays.sort(i);
//獲取該數組中的最大索引的值
System.out.println(i[i.length-1]);