学习了顺序表的查找、插入、删除
package second;
public class List {
int length;
int[] data;
public static final int MAX_LENGTH = 10;
//方法名必须与类名相同
public List() {
length = 0;
data = new int[MAX_LENGTH];
}
public List(int[] paraArray) {
data = new int[MAX_LENGTH];
length = paraArray.length;
for (int i = 0; i < length; i++) {
data[i] = paraArray[i];
}
}
public String toString() {
String resultString = "";
if (length == 0) {
return "empty";
}
for (int i = 0; i < length; i++) {
resultString += data[i] + ", ";
}
return resultString;
}// 写String方法是为了方便后面输出字符串,否则会输出地址
// 为什么要reset?
public void reset() {
length = 0;
}
//查找
public int indexof(int value) {
int position = -1;
for (int i = 0; i < length; i++) {
if (data[i] == value) {
position = i;
break;
}
}
return position;
}
//插入
//为什么用这个方法写出来不会返回列表,只返回了true
public boolean insert(int position, int value) {
if (length == MAX_LENGTH) {
System.out.println("List is Full! ");
return false;
}
if ((position < 0) || (position > length)) {
System.out.println("ERROR");
return false;
}
// 从尾到头
for (int i = length; i > position; i--) {
data[i + 1] = data[i];
}
data[position] = value;
length++;
return true;
}
//删除
public boolean delete(int position) {
if (length == 0) {
System.out.println("List is empty");
return false;
}
if ((position < 0) || (position > length)) {
System.out.println("ERROR! ");
}
// 从头到尾
for (int i = position; i < length - 1; i++) {
data[i] = data[i + 1];
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] Array = { 1, 4, 6, 9 };
List list = new List(Array);
System.out.println("The list of Array is: " + list.toString());
int value = 4;
System.out.println("The position of " + value + " is: " + list.indexof(value));
int position = 2;
value = 5;
System.out.println("After inserting,the list is: " + list.insert(position, value));
position = 3;
System.out.println("After deleting,the list is: " + list.delete(position));
}
}
继昨日发现的插入删除后返回true问题,解决方案如下:
1、输出语句中,不能直接调用insert、delete方法,用先在输出语句前调用,然后再直接输出list;上图中的输出形式相当于只会得到true或false;
2、在运用void的方法时,必要时需要用return来终止函数继续执行,例如下图,在insert方法中,最开始检测错误后,没有return,函数继续执行,最后输出了增加的错误列表,在空位上补的0;
package second;
public class List {
int length;
int[] data;
public static final int MAX_LENGTH = 10;
//方法名必须与类名相同
public List() {
length = 0;
data = new int[MAX_LENGTH];
}
public List(int[] paraArray) {
data = new int[MAX_LENGTH];
length = paraArray.length;
for (int i = 0; i < length; i++) {
data[i] = paraArray[i];
}
}
public String toString() {
String resultString = "";
if (length == 0) {
return "empty";
}
for (int i = 0; i < length; i++) {
resultString += data[i] + ", ";
}
return resultString;
}// 写String方法是为了方便后面输出字符串,否则会输出地址
// 为什么要reset?
public void reset() {
length = 0;
}
public int indexof(int value) {
int position = -1;
for (int i = 0; i < length; i++) {
if (data[i] == value) {
position = i;
break;
}
}
return position;
}
public void insert(int position, int value) {
if (length == MAX_LENGTH) {
System.out.println("List is Full! ");
return;
}
if ((position < 0) || (position > length)) {
System.out.println("ERROR!");
return;// 为什么要return,才会使检测出错误后,列表不会依然增加且添加的0;
// 答:强行终止函数继续运行下面的程序,即运行出错就不应再继续被执行
}
// 从尾到头
for (int i = length; i >= position; i--) {
data[i + 1] = data[i];
// 此处的插入一定要注意i的取值范围,如果是data[i]=data[i-1],则应该>position
// 如果是data[i+1]=data[i],则应该是i>=position;
}
data[position] = value;
length++; // 要注意使列表长度增减,否则会导致数据缺失;
}
public boolean delete(int position) {
if (length == 0) {
System.out.println("List is empty");
return false;
}
if ((position < 0) || (position > length)) {
System.out.println("ERROR! ");
return false;
}
// 从头到尾
for (int i = position; i <= length - 1; i++) {
data[i] = data[i + 1];
}
length--;// 要注意使列表长度减少,否则列表会自己在缺失的位置补0;
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] Array = { 1, 4, 6, 9 };
List list = new List(Array);
System.out.println("The list of Array is: " + list.toString());
int value = 4;
System.out.println("The position of " + value + " is: " + list.indexof(value));
int position = 2;
value = 5;
list.insert(position, value);
System.out.println("After inserting,the list is: " + list);
position = 7;
list.delete(position);
System.out.println("After deleting,the list is: " + list);
position = 8;
value = 10;
list.insert(position, value);
System.out.println("After inserting,the list is: " + list);
position = 3;
list.delete(position);
System.out.println("After deleting,the list is: " + list);
list.reset();
System.out.println("After reset,the list is: " + list);
}
}