🎈数据结构是一门逻辑非常严谨的学科,当我们实际处理一些数据时,要考虑各种会出现的问题。比如在顺序表中增加数据时,首先要考虑容量够不够用,增加的位置是否合理,等等一些问题,然后才是具体增加数据。
🎈数据在内存中存储分为顺序存储(数组)和链式存储(链表),顺序存储就是在内存中分配一块连续的空间来存储一些数据。顺序表就是实现了操作这块内存数据的一些方法。
- public class ArrayList {
- private int[] arr;
- private int usedSize;
- private static final int DEFAULT_SIZE = 10;
- public int getUsedSize() {
- return this.usedSize;
- }
-
- public ArrayList() {
- this.arr = new int[DEFAULT_SIZE];
- }
- public void add(int val) {
-
- //检测容量,不够则扩容
- if(getUsedSize() == arr.length) {
- arr = Arrays.copyOf(this.arr, this.arr.length * 2);
- }
- this.arr[usedSize] = val;
- this.usedSize++;
- }
- public void add(int val, int pos) throws IndexExpection{
- //检测pos位置是否合法
- if(pos < 0 || pos > getUsedSize()) {
- throw new IndexExpection("下标异常");
- }
- //检测容量
- if(getUsedSize() == arr.length) {
- this.arr = Arrays.copyOf(this.arr, this.arr.length * 2);
- }
-
- for(int i = getUsedSize() - 1; i >= pos; i--) {
- this.arr[i + 1] = this.arr[i];
- }
- this.arr[pos] = val;
- this.usedSize++;
-
- }
- public int indexOf(int val) throws EmptyExpection{
- if(isEmpty()) {
- throw new EmptyExpection("空异常");
- }
-
- for(int i = 0; i < getUsedSize(); i++) {
- if(this.arr[i] == val) {
- return i;
- }
- }
- return -1;
- }
- public void remove(int val) throws EmptyExpection{
- //判空
- if(isEmpty()) {
- throw new EmptyExpection("空异常");
- }
- int index = indexOf(val);
- if(index == -1) {
- System.out.println("没有这个数据");
- return;
- }
- for(int i = index; i < getUsedSize() - 1; i++) {
- this.arr[i] = this.arr[i + 1];
- }
- this.usedSize--;
-
- }
- public void clear() throws EmptyExpection{
- if(isEmpty()) {
- throw new EmptyExpection("空异常");
- }
- this.usedSize = 0;
- }
- public boolean contains(int val) throws EmptyExpection{
- if(isEmpty()) {
- throw new EmptyExpection("空异常");
- }
- for(int i = 0; i < getUsedSize(); i++) {
- if(this.arr[i] == val) {
- return true;
- }
- }
- return false;
- }
- public void set(int pos, int val) throws IndexExpection, EmptyExpection{
- if(isEmpty()) {
- throw new EmptyExpection("空异常");
- }
- if(pos < 0 || pos >= getUsedSize()) {
- throw new IndexExpection("下标异常");
- }
- this.arr[pos] = val;
- }
- public int get(int pos) throws IndexExpection, EmptyExpection{
- if(isEmpty()) {
- throw new EmptyExpection("空异常");
- }
- if(pos < 0 || pos >= getUsedSize()) {
- throw new IndexExpection("下标异常");
- }
- return this.arr[pos];
- }
- public boolean isEmpty() {
- return getUsedSize() == 0;
- }
- public void display() throws EmptyExpection{
- if(isEmpty()) {
- throw new EmptyExpection("空异常");
- }
- for(int i = 0; i < getUsedSize(); i++) {
- System.out.print(this.arr[i] + " ");
- }
- }
- }
- public class EmptyExpection extends RuntimeException{
- public EmptyExpection() {
-
- }
- public EmptyExpection(String message) {
- super(message);
- }
- }
- public class IndexExpection extends RuntimeException{
- public IndexExpection() {
-
- }
- public IndexExpection(String message) {
- super(message);
- }
- }
- public class Main {
- public static void main(String[] args) {
- ArrayList arrayList = new ArrayList();
- arrayList.add(1);
- arrayList.add(2);
- arrayList.add(3);
- try {
- arrayList.add(10,0);
-
- int ret = arrayList.indexOf(11);
- arrayList.remove(10);
-
- arrayList.set(0,10);
- int ret2 = arrayList.get(0);
-
- arrayList.display();
-
- }catch (EmptyExpection e) {
- e.printStackTrace();
- }catch (IndexExpection e) {
- e.printStackTrace();
- }
-
- }
- }
🐵我们需大量思考一些问题,锻炼自己这种严谨的逻辑思维,相信会有不一样的收获。