這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Java中定義數(shù)組,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

在很多的面試題上都會出現(xiàn)數(shù)組排序的操作形式。但是這個(gè)時(shí)候你千萬別寫上:java.util.Arrays.sort(數(shù)組)。而這種排序都是以升序?yàn)橹鳌?/p>
范例: 冒泡排序
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();
}
}所謂的轉(zhuǎn)置最簡單的理解就是首尾交換。而如果要想實(shí)現(xiàn)這樣的交換有兩種實(shí)現(xiàn)思路。
思路一:開辟一個(gè)新的等長的數(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ù)組長度 ÷ 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)在可能想到的最簡化的實(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)練。
在之前所定義的數(shù)組都屬于基本數(shù)據(jù)類型數(shù)組,那么對象也可以將其定義為數(shù)組,這樣的操作形式稱為對象數(shù)組。對象數(shù)組往往是以引用數(shù)據(jù)類型為主的定義,例如:類、接口,而且對象數(shù)組也分為兩種定義格式。
對象數(shù)組動態(tài)初始化:類名稱 對象數(shù)組名稱[] = new 類名稱[長度];
對象數(shù)組靜態(tài)初始化:類名稱 對象數(shù)組名稱[] = new 類名稱[]{實(shí)例化對象,…};
范例: 對象數(shù)組的動態(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 {
// 動態(tài)初始化之后對象數(shù)組中的每一個(gè)元素都是其對象數(shù)據(jù)類型的默認(rèn)值
public static void main(String args[]) {
Person per[] = new Person[3]; //動態(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 {
// 動態(tài)初始化之后對象數(shù)組中的每一個(gè)元素都是其對象數(shù)據(jù)類型的默認(rèn)值
public static void main(String args[]) {
Person per[] = new Person[] {
new Person("張三", 1),
new Person("王五", 2),
new Person("李四", 4)
}; //動態(tài)初始化
for(int x = 0; x < per.length; x++) {
System.out.println(per[x].getInfo());
}
}
}每一個(gè)對象可以保存更多的的屬性,所以對象數(shù)組保存的內(nèi)容要比基本數(shù)據(jù)類型更多。那么應(yīng)用的也就更多。所有的開發(fā)必定都存在有對象數(shù)組的概念。
上述就是小編為大家分享的怎么在Java中定義數(shù)組了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱:怎么在Java中定義數(shù)組-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://chinadenli.net/article0/hgpoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站制作、網(wǎng)站排名、靜態(tài)網(wǎng)站、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容