欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Java中一个基于数组的顺序表的增删查改

时间:2023-06-18

目录

定义一个数组

增  

在头部插入

在尾部插入

根据索引插入

给数组扩容

测试

删除头部元素

删除尾部元素

根据索引删除元素

测试

根据索引修改,返回修改前的元素值

测试 

根据元素值查询

根据索引查询

根据元素值查找对应的索引下标

测试 

toString()方法输出

全部代码

 


定义一个数组

public class MyArray { //数组存储元素 private int[] data; //当前数组实际存储的元素个数 private int size; public MyArray(int capacity){ data = new int[capacity]; }}

增  

在头部插入

public void addFirst(int val){ if (size == data.length){ grow(); } for (int i = size-1; i >= 0 ; i--) { data[i+1] = data[i]; } data[0] = val; size++; }

在尾部插入

public void addLast(int val){ //先要判断数组是否满 if(size == data.length){ grow(); } data[size] = val; size++; }

根据索引插入

public void addIndex(int index,int val) { if (size == data.length) { grow(); } if (index < 0 || index > size) { System.err.println("插入错误!"); return; } if (index == 0) { addFirst(val); return;//如果没有return的话,会插入两次 } if (index == size) { addLast(val); } else { for (int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = val; size++; } }

给数组扩容

private void grow(){ int[] newData = Arrays.copyOf(this.data,this.data.length << 1); this.data =newData; }

测试

public class Test { public static void main(String[] args) { MyArray myArray = new MyArray(3); myArray.addLast(1); myArray.addLast(3); myArray.addLast(5); myArray.addLast(7); //[1,3,5,7] System.out.println(myArray); myArray.addFirst(10); //[10,1,3,5,7] System.out.println(myArray); myArray.addIndex(3,8); myArray.addIndex(0,9); myArray.addIndex(6,33); //[9,10,1,3,8,5,33,7] System.out.println(myArray); }}

删除头部元素

public void removeFirst(){ for (int i = 0; i <= size-1 ; i++) { data[i] = data[i+1]; } size--; }

删除尾部元素

public void removeLast(){ removeIndex(size-1); }

根据索引删除元素

public void removeIndex(int index){ if(index < 0 || index >= size){ System.out.println("索引非法!"); return; } for (int i = index; i < size ; i++) { data[i] = data[i+1]; } size--; }

测试

public class Test { public static void main(String[] args) { MyArray myArray = new MyArray(3); myArray.addLast(1); myArray.addLast(3); myArray.addLast(5); myArray.addLast(7); //[1,3,5,7] System.out.println(myArray); myArray.addFirst(10); //[10,1,3,5,7] System.out.println(myArray); myArray.addIndex(3,8); myArray.addIndex(0,9); myArray.addIndex(6,33); //[9,10,1,3,8,5,33,7] System.out.println(myArray); myArray.removeIndex(0); myArray.removeIndex(2); myArray.removeIndex(5); //[10,1,8,5,33] System.out.println(myArray); myArray.removeFirst(); myArray.removeLast(); //[1,8,5] System.out.println(myArray); }}

 

根据索引修改,返回修改前的元素值

public int set(int index,int newVal){ if(index < 0 || index > size-1){ System.out.println("索引非法!"); return -1; } int oldVal = data[index]; data[index] = newVal; return oldVal; }

测试 

public class Test { public static void main(String[] args) { MyArray myArray = new MyArray(3); myArray.addLast(1); myArray.addLast(3); myArray.addLast(5); myArray.addLast(7); //[1,3,5,7] System.out.println(myArray); myArray.addFirst(10); //[10,1,3,5,7] System.out.println(myArray); myArray.addIndex(3,8); myArray.addIndex(0,9); myArray.addIndex(6,33); //[9,10,1,3,8,5,33,7] System.out.println(myArray); myArray.removeIndex(0); myArray.removeIndex(2); myArray.removeIndex(5); //[10,1,8,5,33] System.out.println(myArray); myArray.removeFirst(); myArray.removeLast(); //[1,8,5] System.out.println(myArray); myArray.set(0,3); myArray.set(1,6); myArray.set(2,8); //[3,6,8] System.out.println(myArray); }}

 

根据元素值查询

public boolean contains(int val){ int index =getByVal(val); if(index == -1){ return false; } return true; }

根据索引查询

public int getVal(int index){ if(index < 0 || index > size-1){ System.out.println("索引非法!"); } return data[index]; }

根据元素值查找对应的索引下标

public int getByVal(int val){ for (int i = 0; i < size; i++) { if (data[i] == val){ return i; } } System.out.println("该元素不存在!"); return -1;//该元素不存在 }

测试 

public class Test { public static void main(String[] args) { MyArray myArray = new MyArray(3); myArray.addLast(1); myArray.addLast(3); myArray.addLast(5); myArray.addLast(7); //[1,3,5,7] System.out.println(myArray); myArray.addFirst(10); //[10,1,3,5,7] System.out.println(myArray); myArray.addIndex(3,8); myArray.addIndex(0,9); myArray.addIndex(6,33); //[9,10,1,3,8,5,33,7] System.out.println(myArray); myArray.removeIndex(0); myArray.removeIndex(2); myArray.removeIndex(5); //[10,1,8,5,33] System.out.println(myArray); myArray.removeFirst(); myArray.removeLast(); //[1,8,5] System.out.println(myArray); myArray.set(0,3); myArray.set(1,6); myArray.set(2,8); //[3,6,8] System.out.println(myArray); //false //8 //0 System.out.println(myArray.contains(2)); System.out.println(myArray.getVal(2)); System.out.println(myArray.getByVal(3)); }}

 

toString()方法输出

public String toString() { String ret = "["; for (int i = 0; i < size; i++) { ret += data[i]; if (i != size - 1) { ret += ","; } } ret += "]"; return ret; }

全部代码

import java.util.Arrays;public class MyArray { //数组存储元素 private int[] data; //当前数组实际存储的元素个数 private int size; public MyArray(int capacity){ data = new int[capacity]; } public void addFirst(int val){ if (size == data.length){ grow(); } for (int i = size-1; i >= 0 ; i--) { data[i+1] = data[i]; } data[0] = val; size++; } public void addIndex(int index,int val) { if (size == data.length) { grow(); } if (index < 0 || index > size) { System.err.println("插入错误!"); return; } if (index == 0) { addFirst(val); return;//如果没有return的话,会插入两次 } if (index == size) { addLast(val); } else { for (int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = val; size++; } } public void addLast(int val){ //先要判断数组是否满 if(size == data.length){ grow(); } data[size] = val; size++; } public void removeFirst(){ for (int i = 0; i <= size-1 ; i++) { data[i] = data[i+1]; } size--; } public void removeLast(){ removeIndex(size-1); } public void removeIndex(int index){ if(index < 0 || index >= size){ System.out.println("索引非法!"); return; } for (int i = index; i < size ; i++) { data[i] = data[i+1]; } size--; data[size] = 0; } public int set(int index,int newVal){ if(index < 0 || index > size-1){ System.out.println("索引非法!"); return -1; } int oldVal = data[index]; data[index] = newVal; return oldVal; } public int getByVal(int val){ for (int i = 0; i < size; i++) { if (data[i] == val){ return i; } } return -1;//该元素不存在 } public boolean contains(int val){ int index =getByVal(val);// if(index == -1){ return false; } return true; } public int getVal(int index){ if(index < 0 || index > size-1){ System.out.println("索引非法!"); } return data[index]; } public String toString() { String ret = "["; for (int i = 0; i < size; i++) { ret += data[i]; if (i != size - 1) { ret += ","; } } ret += "]"; return ret; } private void grow(){ int[] newData = Arrays.copyOf(this.data,this.data.length << 1); this.data =newData; }}

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。