0 課程地址
https://coding.imooc.com/lesson/207.html#mid=13410
1 重點關注
1.1 continue,break,return的區別
for循環中,continue結束本輪循環,break結束循環體,return 結束方法
1.2 設計方式:刪除
刪除數組列表等的索引的時候等,要返回刪除元素的值
2 課程內容
見3
3 Coding
3.1 相關coding
邏輯類(重點關注5)
package com.company; import java.util.Arrays; public class Array { private int size; //int類型的數組 private int[] data; //1.1 創建構造函數,傳入容量,則新生成一個數組 public Array(int capacity){ data = new int[capacity]; size = 0; } //1.2 創建無參構造函數 public Array(){ this(10); } //1.3 添加傳入靜態數組的構造函數 public Array(int[] param){ this.data = param; long outParm = Arrays.stream(param).filter(e->{ return e>0; }).count(); this.size = (int)outParm; } //2.1 添加getSize,獲取數組元素個數 public int getSize(){ return size; } //2.2 添加getCapacity,獲取數組容量 public int getCapacity(){ return data.length; } //2.3 添加數組是否爲空方法 public boolean isEmpty(){ return size==0; } //3.1 在數組末尾添加元素 public void addLast(int e){ addElement(size,e); } //3.2 在數組起始添加元素 public void addFirst(int e){ addElement(0,e); } //3.3 數組根據索引添加元素 public void addElement(int index,int e){ //1 校驗異常 //1.1 如果數組已經滿了,則禁止插入 if(size== data.length){ throw new IllegalArgumentException("數組已滿,禁止插入"); } //1.2 如果傳入的索引在已有數組的索引之外,則校驗異常 if(index<0||index>size){ throw new IllegalArgumentException("索引應在已有數組的索引之間"); } //2 實現根據索引添加元素的邏輯 //2.1 data同步 for(int j = size-1;j>=index;j--){ data[j+1] = data[j]; } data[index] = e; //2.2 size同步 size++; } //4.1 數組 toString 範例 @Override public String toString() { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(String.format("Array:size = %d,capacity = %d\n",size,data.length)); stringBuffer.append("["); for(int i=0;i<size;i++){ stringBuffer.append(data[i]); if(i!=size-1){ stringBuffer.append(","); } } stringBuffer.append("]"); return stringBuffer.toString(); } //4.2 get獲取元素 public int get(int index){ if(index<0||index>data.length){ throw new IllegalArgumentException("111"); } return data[index]; } //4.3 set獲取元素 public void set(int index,int e){ if(index<0||index>data.length){ throw new IllegalArgumentException("111"); } data[index] = e; } //5.1 數組包含 public boolean contails(int e){ for(int i = 0;i<size;i++){ if(e==data[i]){ return true; } } return false; } //5.2 數組搜索 public int search(int e){ for(int i = 0;i<size;i++){ if(e==data[i]){ return i; } } return -1; } //5.3 數組刪除,通常情況下做刪除,會在出參把刪除的值帶出來 public int remove(int index){ if(index<0||index>=size){ throw new IllegalArgumentException("111"); } int outParm = data[index]; for(int i=index;i<size-1;i++){ data[i] = data[i+1]; } //這塊不塞值也沒有任何影響,因爲size已經--了,不會訪問到size之外的元素 data[size-1]= 0; size--; return outParm; } //5.4 刪除首個元素 public int removFirst(){ return remove(0); } //5.5 刪除最後的元素 public int removLast(){ return remove(size-1); } //5.6 刪除指定的元素 public void removElement(int e){ int index = -1; //判斷刪除的元素是否存在 for(int i=0;i<size;i++){ if(e==data[i]){ index = i; break; } } if(index>=0){ remove(index); }else{ throw new IllegalArgumentException("刪除的元素未找到"); } } }
測試類
public static void main(String[] args) { int[] parm = new int[]{1,2,3,4,4,5,6,7}; Array array = new Array(parm); System.out.println(array.contails(7)); System.out.println(array.contails(8)); System.out.println(array.search(4)); System.out.println(array.search(8)); System.out.println(array.remove(1)); System.out.println(array); System.out.println(array.removFirst()); System.out.println(array); System.out.println(array.removLast()); System.out.println(array); array.removElement(4); System.out.println(array); array.removElement(8); }