Java如何自定義類(lèi)數(shù)組的創(chuàng)建和初始化
自定義類(lèi)數(shù)組的創(chuàng)建和初始化
剛剛在慕課學(xué)習(xí)Java的集合類(lèi)List過(guò)程中,向集合中添加元素時(shí),遇到一個(gè)問(wèn)題:
定義了一個(gè)Course類(lèi)
public class Course {
private String id;
private String name; //課程名稱
//get set方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
在測(cè)試類(lèi)中有一個(gè)Course類(lèi)的List集合allCourses,一個(gè)向該List集合添加元素的方法addToAllCourses() ;
public class ListTest {
private List allCourses; //用于存放備選課程的List
//構(gòu)造函數(shù),初始化allCourses
public ListTest() {
this.allCourses = new ArrayList();
}
public void addToAllCourses(String id, String name) {
//List的add(Object e), 添加一個(gè)元素
Course cs = new Course(); //創(chuàng)建一個(gè)Course對(duì)象,并設(shè)置其參數(shù)
cs.setId(id);
cs.setName(name);
allCourses.add(cs);
// List的addAll(Collection c)方法
Course[] courses = new Course[3];
courses[0].setId("2");
courses[0].setName("C語(yǔ)言");
courses[1].setId("3");
courses[1].setName("數(shù)據(jù)庫(kù)");
courses[2].setId("4");
courses[2].setName("計(jì)算機(jī)網(wǎng)絡(luò)");
allCourses.addAll(Arrays.asList(courses));
//在該方法中 參數(shù)必須為collection類(lèi)型,因此必須用工具類(lèi)進(jìn)行類(lèi)型轉(zhuǎn)換
}
}
主函數(shù)測(cè)試
public static void main(String[] args) {
ListTest list = new ListTest();
list.addToAllCourses("1", "數(shù)據(jù)結(jié)構(gòu)");
List<Course> li = list.getAllCourses();
for(int i = 0; i < li.size(); i++) {
System.out.println((i + 1) + ": " + li.get(i).getId()
+ " " + li.get(i).getName());
}
}
乍看是沒(méi)有問(wèn)題的,但是一運(yùn)行,問(wèn)題就來(lái)了,myeclipse報(bào)出了空指針異常。異常拋出點(diǎn)為addToAllCourses()方法中 Course類(lèi)數(shù)組courses在賦值時(shí)的代碼。
既然是空指針異常,也就是說(shuō),courses[0], courses[1], courses[2]都是沒(méi)有被初始化的。
一般而言,如下的數(shù)組定義在myeclipse中是不會(huì)報(bào)錯(cuò)的:
String[] s = new String[3]; s[0] = "000000"; System.out.println(s[0]);
但是,我的代碼中,數(shù)組的類(lèi)型為自定義的類(lèi),而非Java本身提供的類(lèi)(如String類(lèi)),因而我懷疑是不是我的數(shù)組定義出了問(wèn)題. 查閱資料后發(fā)現(xiàn),自定義類(lèi)的數(shù)組定義后的初始化應(yīng)該如下:
Course[] courses = new Course[3];
courses[0] = new Course();
courses[0].setName("0000000");
System.out.println(courses[0].getName());
也就是說(shuō),在聲明了自定義類(lèi)的數(shù)組之后,對(duì)每一個(gè)數(shù)組元素的初始化,都要為其new一個(gè)對(duì)象出來(lái)使得指針指向該對(duì)象,Java語(yǔ)言本身是不提供在自定義類(lèi)數(shù)組聲明時(shí)候自動(dòng)創(chuàng)建新對(duì)象的方式的。
此處順便再補(bǔ)充一下類(lèi)二維數(shù)組的定義及初始化,
/*Java提供類(lèi)*/
//方式一:
String[][] s = new String[][] { {"a", "b", "c"},
{"d", "e", "f"} };
//方式二
int r = 0;
String[][] s = new String[2][3];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++) {
s[i][j] = String.valueOf(r++);
}
/*自定義類(lèi)*/
Course[][] courses = new Course[2][3]; //聲明
courses[0][0] = new Course(); //使用時(shí)new一個(gè)實(shí)例
courses[0][0].setId("0");
courses[0][0].setName("000000");
System.out.println(courses[0][0].getId() + " "
+ courses[0][0].getName());
//測(cè)試 不報(bào)空指針異常
自定義類(lèi)封裝數(shù)組,添加類(lèi)方法實(shí)現(xiàn)數(shù)據(jù)
1、具體見(jiàn)注釋
2、后續(xù)或有更新
public class MyArray {
private long[] array;
private int cnt; // 自定義數(shù)組類(lèi)的元素個(gè)數(shù)
/**
使用自定義類(lèi)封裝數(shù)組,添加類(lèi)方法實(shí)現(xiàn)數(shù)據(jù)操作
*/
public MyArray() {
array = new long[50];
}
public MyArray(int size) {
array = new long[size];
}
/**
插入數(shù)據(jù),返回值為空
*/
public void insert(long insertValue) {
array[cnt++] = insertValue;
}
/**
顯示數(shù)據(jù),返回值為空
*/
public void display() {
System.out.print("[");
for (int i = 0; i < cnt ; ++i) {
System.out.print(array[i]);
if (i != cnt - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
/**
按值查找數(shù)據(jù),返回索引值
算法:線性查找
*/
public int search(long targetValue) {
int i;
int searchResult;
for (i = 0; i < cnt; ++i) {
if (targetValue == array[i]) {
break;
}
}
if (i == cnt) {
searchResult = -1;
} else {
searchResult = i;
}
return searchResult; // 保持單一出口
}
/**
按索引查找數(shù)據(jù),返回值為目標(biāo)數(shù)據(jù)
*/
public long get(int targetIndex) {
if (targetIndex < 0 || targetIndex >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
return array[targetIndex];
}
}
/**
按值刪除數(shù)據(jù),返回其索引值
*/
public int deleteByValue(long deleteValue) {
int i;
int deleteResult;
for (i = 0; i < cnt; ++i) {
if (array[i] == deleteValue) {
int j;
for (j = i; j < cnt-1; ++j) {
array[j] = array[j+1];
}
array[j] = array[--cnt];
break; // 僅刪除從左到右第一個(gè)找到的目標(biāo)值
}
}
if (i == cnt) {
deleteResult = -1;
} else {
deleteResult = i;
}
return deleteResult; // 保持單一出口
}
/**
按索引刪除數(shù)據(jù),返回值為空
*/
public void delete(int index) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
int i;
for (i = index; i < cnt - 1; ++i) {
array[i] = array[i + 1];
}
//array[i] = array[cnt - 1];
//cnt--;
array[i] = array[--cnt]; // 替換上兩行
}
}
/**
根據(jù)索引值,更新數(shù)據(jù),返回值為空
*/
public void update(int index, int newValue) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
array[index] = newValue;
}
}
public static void main(String[] args) {
MyArray array = new MyArray(3);
array.insert(13);
array.insert(34);
array.insert(90);
array.display();
array.deleteByValue(34);
array.display();
}
}
3、添加自定義有序數(shù)組類(lèi)
public class MyOrderArray {
private long[] array;
private int cnt; // 自定義數(shù)組類(lèi)的元素個(gè)數(shù)
/**
使用自定義類(lèi)封裝數(shù)組,添加類(lèi)方法實(shí)現(xiàn)數(shù)據(jù)操作
*/
public MyOrderArray() {
array = new long[50];
}
public MyOrderArray(int size) {
array = new long[size];
}
/**
按序插入數(shù)據(jù),返回值為空
*/
public void insert(long insertValue) {
int i;
for (i = 0; i < cnt; ++i) {
if (array[i] > insertValue) {
break;
}
}
int j;
for (j = cnt; j > i; --j) {
array[j] = array[j - 1];
}
array[i] = insertValue;
cnt++;
}
/**
顯示數(shù)據(jù),返回值為空
*/
public void display() {
System.out.print("[");
for (int i = 0; i < cnt ; ++i) {
System.out.print(array[i]);
if (i != cnt - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
/**
按值查找數(shù)據(jù),返回索引值
算法:線性查找
*/
public int search(long targetValue) {
int i;
int searchResult;
for (i = 0; i < cnt; ++i) {
if (targetValue == array[i]) {
break;
}
}
if (i == cnt) {
searchResult = -1;
} else {
searchResult = i;
}
return searchResult; // 保持單一出口
}
/**
按索引查找數(shù)據(jù),返回值為目標(biāo)數(shù)據(jù)
*/
public long get(int targetIndex) {
if (targetIndex < 0 || targetIndex >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
return array[targetIndex];
}
}
/**
按值刪除數(shù)據(jù),返回其索引值
*/
public int deleteByValue(long deleteValue) {
int i;
int deleteResult;
for (i = 0; i < cnt; ++i) {
if (array[i] == deleteValue) {
int j;
for (j = i; j < cnt-1; ++j) {
array[j] = array[j+1];
}
array[j] = array[--cnt];
break; // 僅刪除從左到右第一個(gè)找到的目標(biāo)值
}
}
if (i == cnt) {
deleteResult = -1;
} else {
deleteResult = i;
}
return deleteResult; // 保持單一出口
}
/**
按索引刪除數(shù)據(jù),返回值為空
*/
public void delete(int index) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
int i;
for (i = index; i < cnt - 1; ++i) {
array[i] = array[i + 1];
}
//array[i] = array[cnt - 1];
//cnt--;
array[i] = array[--cnt]; // 替換上兩行
}
}
/**
根據(jù)索引值,更新數(shù)據(jù),返回值為空
*/
public void update(int index, int newValue) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
array[index] = newValue;
}
}
public static void main(String[] args) {
MyOrderArray array = new MyOrderArray(3);
array.insert(90);
array.insert(13);
array.insert(34);
array.display();
array.deleteByValue(34);
array.display();
}
}
4、MyArray類(lèi)與MyOrderArray類(lèi)目前僅區(qū)別于insert方法,后續(xù)或有更新
5、MyOrderArray類(lèi)新增二分查找方法binarySearch,具體細(xì)節(jié)見(jiàn)該方法代碼
public class MyOrderArray {
private long[] array;
private int cnt; // 自定義數(shù)組類(lèi)的元素個(gè)數(shù)
/**
使用自定義類(lèi)封裝數(shù)組,添加類(lèi)方法實(shí)現(xiàn)數(shù)據(jù)操作
*/
public MyOrderArray() {
array = new long[50];
}
public MyOrderArray(int size) {
array = new long[size];
}
/**
按序插入數(shù)據(jù),返回值為空
*/
public void insert(long insertValue) {
int i;
for (i = 0; i < cnt; ++i) {
if (array[i] > insertValue) {
break;
}
}
int j;
for (j = cnt; j > i; --j) {
array[j] = array[j - 1];
}
array[i] = insertValue;
cnt++;
}
/**
顯示數(shù)據(jù),返回值為空
*/
public void display() {
System.out.print("[");
for (int i = 0; i < cnt ; ++i) {
System.out.print(array[i]);
if (i != cnt - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
/**
按值查找數(shù)據(jù),返回索引值
算法:線性查找
*/
public int search(long targetValue) {
int i;
int searchResult;
for (i = 0; i < cnt; ++i) {
if (targetValue == array[i]) {
break;
}
}
if (i == cnt) {
searchResult = -1;
} else {
searchResult = i;
}
return searchResult; // 保持單一出口
}
/**
按值查找數(shù)據(jù),返回索引值
算法:二分查找
*/
public int binarySearch(long targetValue) {
int middle = 0;
int low = 0;
int top = cnt;
while (true) {
middle = (top + low) / 2;
if (targetValue == array[middle]) {
return middle;
} else if (low > top) {
return -1;
} else if (targetValue < array[middle]) {
top = middle - 1; // 切記減一
} else if (targetValue >= array[middle]) {
low = middle + 1; // 切記加一
}
}
}
/**
按索引查找數(shù)據(jù),返回值為目標(biāo)數(shù)據(jù)
*/
public long get(int targetIndex) {
if (targetIndex < 0 || targetIndex >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
return array[targetIndex];
}
}
/**
按值刪除數(shù)據(jù),返回其索引值
*/
public int deleteByValue(long deleteValue) {
int i;
int deleteResult;
for (i = 0; i < cnt; ++i) {
if (array[i] == deleteValue) {
int j;
for (j = i; j < cnt-1; ++j) {
array[j] = array[j+1];
}
array[j] = array[--cnt];
break; // 僅刪除從左到右第一個(gè)找到的目標(biāo)值
}
}
if (i == cnt) {
deleteResult = -1;
} else {
deleteResult = i;
}
return deleteResult; // 保持單一出口
}
/**
按索引刪除數(shù)據(jù),返回值為空
*/
public void delete(int index) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
int i;
for (i = index; i < cnt - 1; ++i) {
array[i] = array[i + 1];
}
//array[i] = array[cnt - 1];
//cnt--;
array[i] = array[--cnt]; // 替換上兩行
}
}
/**
根據(jù)索引值,更新數(shù)據(jù),返回值為空
*/
public void update(int index, int newValue) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
array[index] = newValue;
}
}
public static void main(String[] args) {
MyOrderArray array = new MyOrderArray(3);
array.insert(90);
array.insert(13);
array.insert(34);
array.display();
//array.deleteByValue(34);
System.out.println(array.binarySearch(90));
array.display();
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
一篇文章帶你了解一些Java反射的學(xué)習(xí)記錄
java反射機(jī)制是一個(gè)很好用的東西,用它可以解決很多死的東西,因?yàn)榉瓷錂C(jī)制的靈活行很大,有了他,我們就不要花太多的時(shí)間來(lái)寫(xiě)操做數(shù)據(jù)庫(kù)的代碼了,這個(gè)可以很大的減少開(kāi)發(fā)時(shí)間,而且代碼的可讀性好2021-09-09
Java SpringMVC 集成靜態(tài)資源的方式你了解嗎
本篇文章主要介紹了SpringMVC集成靜態(tài)資源的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-10-10
springmvc—handlermapping三種映射方式
這篇文章主要介紹了springmvc—handlermapping三種映射方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

