Java中數(shù)組的定義和使用教程(三)
數(shù)組排序
在很多的面試題上都會(huì)出現(xiàn)數(shù)組排序的操作形式。但是這個(gè)時(shí)候你千萬別寫上:java.util.Arrays.sort(數(shù)組)。而這種排序都是以升序?yàn)橹鳌?/p>
基礎(chǔ)的排序操作:
范例: 冒泡排序
public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {9, 3, 1, 5, 4, 2, 7, 8, 6, 0};
sort(data);
printArray(data);
}
public static void sort(int arr[]) { //實(shí)現(xiàn)數(shù)組排序
for(int x = 0; x < arr.length - 1; x++) {
for(int y = 0; y < arr.length - x - 1; y++) {
if(arr[y] > arr[y+1]) {
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}
//定義一個(gè)專門進(jìn)行數(shù)組輸出的方法
public static void printArray(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
System.out.println();
}
}
數(shù)組轉(zhuǎn)置
所謂的轉(zhuǎn)置最簡(jiǎn)單的理解就是首尾交換。而如果要想實(shí)現(xiàn)這樣的交換有兩種實(shí)現(xiàn)思路。
思路一:開辟一個(gè)新的等長(zhǎng)的數(shù)組,而后將原始數(shù)組倒序保存進(jìn)去;
public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {9, 3, 1, 5, 4, 2, 7, 8, 6, 0};
data = reverse(data); //反轉(zhuǎn)
printArray(data);
}
public static int [] reverse(int arr[]) {
int temp[] = new int[arr.length];
int foot = 0;
for(int x = arr.length - 1; x >= 0; x--) {
temp[foot++] = arr[x];
}
return temp;
}
//定義一個(gè)專門進(jìn)行數(shù)組輸出的方法
public static void printArray(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
System.out.println();
}
}
使用此類模式實(shí)現(xiàn)的最大問題在于開辟了兩塊相同的堆內(nèi)存空間,所以造成空間浪費(fèi)。
思路二:在一個(gè)數(shù)組上完成轉(zhuǎn)換
public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {9, 3, 1, 5, 4, 2, 7, 8, 6, 0};
reverse(data); //反轉(zhuǎn)
printArray(data);
}
public static void reverse(int arr[]) {
int center = arr.length / 2; //轉(zhuǎn)換次數(shù)
int head = 0; //頭部開始索引
int tail = arr.length - 1; //尾部開始索引
for(int x = 0; x < center; x++) {
int temp = arr[head];
arr[head] = arr[tail];
arr[tail] = temp;
head++;
tail--;
}
}
//定義一個(gè)專門進(jìn)行數(shù)組輸出的方法
public static void printArray(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
System.out.println();
}
}
這種轉(zhuǎn)換只需要根據(jù)數(shù)組長(zhǎng)度 ÷ 2即可。
如果要進(jìn)行二維數(shù)組的原地轉(zhuǎn)置,那么肯定有一個(gè)前提:行列要相等。
范例: 保證中間軸不變(x = y)
public class ArrayDemo {
public static void main(String args[]) {
int data[][] = new int[][] {{1, 2, 3}, {4, 5, 6},{7, 8, 9}};
reverse(data); //反轉(zhuǎn)
printArray(data);
}
public static void reverse(int arr[][]) {
for(int x = 0; x < arr.length; x++) {
for(int y = x; y < arr[x].length; y++) {
if(x != y) {
int temp = arr[x][y];
arr[x][y] = arr[y][x];
arr[y][x] = temp;
}
}
}
}
//定義一個(gè)專門進(jìn)行數(shù)組輸出的方法
public static void printArray(int temp[][]) {
for (int i = 0; i < temp.length; i++) {
for(int j = 0; j < temp[i].length; j++) {
System.out.print(temp[i][j] + "、");
}
System.out.println();
}
System.out.println();
}
}
二分查找法
如果現(xiàn)在要求在一個(gè)指定的數(shù)組之中查詢一個(gè)數(shù)據(jù)的位置,那么現(xiàn)在可能想到的最簡(jiǎn)化的實(shí)現(xiàn),整體數(shù)組遍歷。
范例: 順序查找
public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8};
int search = 7;
System.out.println(index(data, search));
}
public static int index(int arr[], int key) {
for(int x = 0; x < arr.length; x++) {
if(arr[x] == key)
return x;
}
return -1;
}
}
這個(gè)的時(shí)間復(fù)雜度是n,也就是說所有的數(shù)組中的數(shù)據(jù)都需要進(jìn)行一次遍歷,這樣才能確認(rèn)所需要查找的數(shù)據(jù)是否存在,那么現(xiàn)在如果想進(jìn)行更快速地查找,最好的做法是進(jìn)行二分查找(折半查找)。
范例: 實(shí)現(xiàn)二分查找(采用遞歸)
public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8};
int search = 7;
System.out.println(index(data, search));
}
public static int binarySearch(int arr[], int from, int to, int key) {
if(from < to) {
int mid = from / 2 + to / 2; //確定中間點(diǎn)
if(arr[mid] = key) { //數(shù)據(jù)找到了
return mid; // 取得當(dāng)前索引
}else if(key < arr[mid]) {
return binarySearch(arr, from, mid - 1; key);
}
else(key > arr[mid]){
return binarySearch(arr, mid + 1, to, key);
}
}
return -1;
}
}
但是這些都是屬于數(shù)據(jù)結(jié)構(gòu)課程的范圍,是邏輯思維訓(xùn)練。
對(duì)象數(shù)組(核心)
在之前所定義的數(shù)組都屬于基本數(shù)據(jù)類型數(shù)組,那么對(duì)象也可以將其定義為數(shù)組,這樣的操作形式稱為對(duì)象數(shù)組。對(duì)象數(shù)組往往是以引用數(shù)據(jù)類型為主的定義,例如:類、接口,而且對(duì)象數(shù)組也分為兩種定義格式。
- 對(duì)象數(shù)組動(dòng)態(tài)初始化:類名稱 對(duì)象數(shù)組名稱[] = new 類名稱[長(zhǎng)度];
- 對(duì)象數(shù)組靜態(tài)初始化:類名稱 對(duì)象數(shù)組名稱[] = new 類名稱[]{實(shí)例化對(duì)象,…};
范例: 對(duì)象數(shù)組的動(dòng)態(tài)初始化
class Person {
private String name;
private int age;
public Person(String n, int a) {
name = n;
age = a;
}
public String getInfo() {
return "姓名:" + name + ",年齡:" + age;
}
}
public class ArrayDemo {
// 動(dòng)態(tài)初始化之后對(duì)象數(shù)組中的每一個(gè)元素都是其對(duì)象數(shù)據(jù)類型的默認(rèn)值
public static void main(String args[]) {
Person per[] = new Person[3]; //動(dòng)態(tài)初始化
per[0] = new Person("張三", 1);
per[1] = new Person("王五", 2);
per[2] = new Person("李四", 4);
for(int x = 0; x < per.length; x++) {
System.out.println(per[x].getInfo());
}
}
}
范例: 靜態(tài)初始化
class Person {
private String name;
private int age;
public Person(String n, int a) {
name = n;
age = a;
}
public String getInfo() {
return "姓名:" + name + ",年齡:" + age;
}
}
public class ArrayDemo {
// 動(dòng)態(tài)初始化之后對(duì)象數(shù)組中的每一個(gè)元素都是其對(duì)象數(shù)據(jù)類型的默認(rèn)值
public static void main(String args[]) {
Person per[] = new Person[] {
new Person("張三", 1),
new Person("王五", 2),
new Person("李四", 4)
}; //動(dòng)態(tài)初始化
for(int x = 0; x < per.length; x++) {
System.out.println(per[x].getInfo());
}
}
}
每一個(gè)對(duì)象可以保存更多的的屬性,所以對(duì)象數(shù)組保存的內(nèi)容要比基本數(shù)據(jù)類型更多。那么應(yīng)用的也就更多。所有的開發(fā)必定都存在有對(duì)象數(shù)組的概念。
總結(jié)
到此這篇關(guān)于Java中數(shù)組的定義和使用的文章就介紹到這了,更多相關(guān)Java數(shù)組的定義和使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Map中實(shí)現(xiàn)key唯一不重復(fù)操作
這篇文章主要介紹了在Map中實(shí)現(xiàn)key唯一不重復(fù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java編程實(shí)現(xiàn)非對(duì)稱加密的方法詳解
這篇文章主要介紹了Java編程實(shí)現(xiàn)非對(duì)稱加密的方法,簡(jiǎn)單講述了非對(duì)稱加密的概念、原理,并結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)DH加密解密、RSA加密解密、ElGamal加密等具體操作技巧,需要的朋友可以參考下2017-08-08
Spring 實(shí)現(xiàn)給Bean屬性注入null值
這篇文章主要介紹了Spring 實(shí)現(xiàn)給Bean屬性注入null值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot項(xiàng)目中使用@Scheduled讀取動(dòng)態(tài)參數(shù)
這篇文章主要介紹了SpringBoot項(xiàng)目中使用@Scheduled讀取動(dòng)態(tài)參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Rxjava+Retrofit+MVP實(shí)現(xiàn)購物車功能
這篇文章主要為大家詳細(xì)介紹了Rxjava+Retrofit+MVP實(shí)現(xiàn)購物車功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Java源碼解析CopyOnWriteArrayList的講解
今天小編就為大家分享一篇關(guān)于Java源碼解析CopyOnWriteArrayList的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
java返回集合為null還是空集合及空集合的三種寫法小結(jié)
這篇文章主要介紹了java返回集合為null還是空集合及空集合的三種寫法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

