(一)简述
- 实现数组元素的增删改查
(二)参考知识点
-
java 语法
import依赖 构造方法 抛出异常 if条件判断 for循环 while循环 String[] args 参数 &(与) &&(短路与)|(或) ||(短路或) !(非)
-
方法笔记
System.arraycopy 方法复制数组
array.length 方法获取数组长度
Scanner 读取键盘输入
-
String 的 equals 与 "=="
堆栈:创建对象时,堆内存区存储对象数据,栈内存区存储对象的引用(地址) ==:比较的是对象的地址 equals: String 对象的 equals 方法被重写,当地址不同时,进一步比较值
-
String StringBuilder StringBuffer
String: 适合少量字符串操作 StringBuilder: 线程不安全,适合单线程,字符串缓冲区大量操作时 StringBuffer: 线程安全(许多方法带synchronized关键字),适合多线程,大量操作 转换:构造方法(new) append方法 toString方法
-
其他
-
shell 脚本封装编译和执行过程
#! /bin/bash javac -encoding "UTF-8" MyArray.java # -encoding 参数适时添加 java MyArray 注:同时可运用于c语言的编译、链接与执行
-
Java 调用操作系统命令
Process process = Runtime.getRuntime().exec("rm -f test.file"); 注:单独创建进程执行,返回Process对象
-
(三)代码实现
3.1 代码实现
import java.lang.*;
import java.util.*;
public class MyArray{
private int[] array;
private int size;
public MyArray(int capacity){
this.array = new int[capacity];
size = 0;
}
//insert
public void insert(int element, int index) throws Exception {
if(index<0 || index>size){
throw new IndexOutOfBoundsException("Index out of array bounds");
}
if(size >= array.length){
resize();
}
for(int i=size-1;i>=index;i--){
array[i+1] = array[i];
}
array[index] = element;
size++;
System.out.println("Element: "+element+" add successfully");
}
//delete
public int delete(int index) throws Exception{
if(index<0 || index > size-1){
throw new IndexOutOfBoundsException("Index out of array bounds");
}
int delElement = array[index];
for(int i=index+1;i<=size-1;i++){
array[i-1] = array[i];
}
size --;
System.out.println("Element: "+delElement+" delete successfully");
return delElement;
}
//modify
public int modify(int newElement, int index) throws Exception{
if(index<0 || index >size-1){
throw new IndexOutOfBoundsException("Index out of array bounds");
}
int oldElement = array[index];
array[index] = newElement;
System.out.println("Array member index:"+index+" have changed");
System.out.println("the old value is "+oldElement);
System.out.println("the new value is "+newElement);
return oldElement;
}
//get
public int get(int index) throws Exception {
if(index<0 || index>size-1){
throw new IndexOutOfBoundsException("Index out of array bounds");
}
System.out.println("The element of index "+index+" is "+array[index]);
return array[index];
}
public void resize(){
int[] arrayNew = new int[array.length*2];
System.arraycopy(array, 0, arrayNew, 0, array.length);
array = arrayNew;
}
public void output(){
for(int i=0;i<=size-1;i++){
System.out.println("value"+i+" : "+array[i]);
}
}
public static void main(String[] args) throws Exception {
MyArray myArray = new MyArray(4);
String choice = "y";
while(choice.equals("y") || choice.equals("Y")){
System.out.println("continue?(y/n):");
Scanner sc = new Scanner(System.in);
if(sc.hasNext()){
String input = sc.next();
choice = input;
}
//sc.close();
}
myArray.insert(1,0);
myArray.insert(2,1);
myArray.insert(3,2);
myArray.delete(0);
myArray.modify(5,0);
myArray.get(1);
System.out.println("the size of the array is:"+myArray.size);
myArray.output();
}
}
3.2 代码指引
- element 元素值
- index 索引(从0开始编号)
- insert() 增
- delete() 删
- modify() 改
- get() 查
- resize() 数组扩容
- output() 输出
3.3 待完善
- 可利用 Scanner 写成命令交互模式(因时间关系搁置)