Java中數(shù)組的定義和使用教程(二)
數(shù)組與方法調(diào)用
數(shù)組是一個(gè)引用數(shù)據(jù)類型,那么所有的引用數(shù)據(jù)類型都可以為其設(shè)置多個(gè)棧內(nèi)存指向。所以在進(jìn)行數(shù)組操作的時(shí)候,也可以將其通過方法進(jìn)行處理。
范例: 方法接受數(shù)組
public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {1, 2, 3};
printArray(data);
}
//定義一個(gè)專門進(jìn)行數(shù)組輸出的方法
public static void printArray(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
}
}
在方法的參數(shù)上由于需要接受一個(gè)整型數(shù)組,所以就實(shí)現(xiàn)了一個(gè)最為基礎(chǔ)的引用傳遞操作。
范例: 方法返回?cái)?shù)組
public class ArrayDemo {
public static void main(String args[]) {
int data[] = init(); //接受數(shù)組
printArray(data);
}
//此時(shí)的方法希望可以返回一個(gè)數(shù)組類型,所以返回值類型定義為整形數(shù)組
public static int[] init() {
return new int[] {1, 2, 3, 4, 6};
}
//定義一個(gè)專門進(jìn)行數(shù)組輸出的方法
public static void printArray(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
}
}
那么現(xiàn)在的數(shù)組上發(fā)生了引用傳遞,那么也就意味著方法接受數(shù)組之后也可以對(duì)數(shù)組進(jìn)行內(nèi)容修改。
范例: 定義一個(gè)方法,該方法可以實(shí)現(xiàn)數(shù)組的內(nèi)容的乘2
public class ArrayDemo {
public static void main(String args[]) {
int data[] = init();
inc(data);
printArray(data);
}
public static void inc(int arr[]) {
for(int x = 0; x < arr.length; x++)
arr[x] *= 2;
}
//此時(shí)的方法希望可以返回一個(gè)數(shù)組類型,所以返回值類型定義為整形數(shù)組
public static int[] init() {
return new int[] {1, 2, 3, 4, 6};
}
//定義一個(gè)專門進(jìn)行數(shù)組輸出的方法
public static void printArray(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
}
}

Java對(duì)數(shù)組的支持
在Java本身給出的類庫之中也提供有對(duì)于數(shù)組的操作的相關(guān)支持方法。
1、數(shù)組排序:java.util.Arrays.sort(數(shù)組名稱);
范例: 實(shí)現(xiàn)數(shù)組排序操作
public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {5, 13, 1, 55, 77};
char arr[] = new char[] {'D', 'C', 'F'};
java.util.Arrays.sort(data);
java.util.Arrays.sort(arr);
printArray(data);
printArray(arr);
}
//定義一個(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();
}
public static void printArray(char temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "、");
}
System.out.println();
}
}
只要是基本數(shù)據(jù)類型的數(shù)組,Arrays.sort()都可以輕松地實(shí)現(xiàn)排序處理。
2、數(shù)組拷貝:指的是將一個(gè)數(shù)組的部分內(nèi)容替換掉另外一個(gè)數(shù)組的內(nèi)容
方法(加工):System.arraycopy(原數(shù)組名稱, 原數(shù)組開始點(diǎn), 目標(biāo)數(shù)組名稱, 目標(biāo)數(shù)組開始點(diǎn), 拷貝長(zhǎng)度)
范例: 實(shí)現(xiàn)數(shù)組拷貝
原數(shù)組A:1、2、3、4、5、6、7、8、9;
原數(shù)組B:11、22、33、44、55、66、77、88、99;
替換后的數(shù)組A:1、55、66、77、5、6、7、8、9;
public class ArrayDemo {
public static void main(String args[]) {
int dataA[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
int dataB[] = new int[] {11, 22, 33, 44, 55, 66, 77, 88, 99};
System.arraycopy(dataB, 4, dataA, 1, 3);
printArray(dataA);
}
//定義一個(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ù)組操作只能夠作為邏輯玩玩,開發(fā)用不上。
數(shù)組數(shù)據(jù)統(tǒng)計(jì)
現(xiàn)在假設(shè)給你一個(gè)數(shù)組,要求可以統(tǒng)計(jì)該數(shù)組的最大值、最小值、平均值、總和。這種操作肯定是要通過循環(huán)的操作形式完成。
范例: 基本實(shí)現(xiàn)
public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {1, 2, 3, 4, 66, 5, 6, 7, 8, 9};
int max = data[0];
int min = data[0];
int sum = 0;
for(int x = 0; x < data.length; x++) {
sum += data[x];
if(data[x] > max)
max = data[x];
if(data[x] < min)
min = data[x];
}
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
System.out.println("數(shù)據(jù)總和:" + sum);
System.out.println("平均值:" + (double)sum/data.length);
}
}
此時(shí)確實(shí)實(shí)現(xiàn)了所需要的功能,但是隨之會(huì)發(fā)現(xiàn)主方法中的代碼有些多。主方法實(shí)際上就相當(dāng)于一個(gè)客戶端調(diào)用,那么既然是客戶端調(diào)用,里面的代碼應(yīng)該也越簡(jiǎn)單越好。
范例: 改進(jìn)代碼
public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[] {1, 2, 3, 4, 66, 5, 6, 7, 8, 9};
double result[] = stat(data);
System.out.println("最大值:" + result[0]);
System.out.println("最小值:" + result[1]);
System.out.println("數(shù)據(jù)總和:" + result[2]);
System.out.println("平均值:" + result[3]);
}
//此時(shí)需要返回的數(shù)據(jù)一共有四個(gè),那么一個(gè)方法只能夠返回一種數(shù)據(jù)類型,所以應(yīng)該使用數(shù)組返回
//數(shù)組[0]為最大值、數(shù)組[1]為最小值、數(shù)組[2]為數(shù)據(jù)總和、數(shù)組[3]為平均值
public static double[] stat(int data[]) {
double reData[] = new double[4];
reData[0] = data[0];
reData[1] = data[0];
reData[2] = data[0];
for(int x = 1; x < data.length; x++) {
reData[2] += data[x];
if(data[x] > reData[0])
reData[0] = data[x];
if(data[x] < reData[1])
reData[1] = data[x];
}
reData[3] = reData[2] / data.length;
return reData;
}
}
在整個(gè)進(jìn)行程序開發(fā)的時(shí)候,主方法不要涉及到過于復(fù)雜的邏輯程序,只需要關(guān)注結(jié)果。
總結(jié)
到此這篇關(guān)于Java中數(shù)組的定義和使用的文章就介紹到這了,更多相關(guān)Java數(shù)組的定義和使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Hibernate框架數(shù)據(jù)分頁技術(shù)實(shí)例分析
這篇文章主要介紹了Hibernate框架數(shù)據(jù)分頁技術(shù),結(jié)合實(shí)例形式分析了Hibernate框架實(shí)現(xiàn)數(shù)據(jù)分頁的原理,步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03
關(guān)于struts返回對(duì)象json格式數(shù)據(jù)的方法
以下為大家介紹,關(guān)于struts返回對(duì)象json格式數(shù)據(jù)的方法,希望對(duì)有需要的朋友有所幫助。2013-04-04
java實(shí)現(xiàn)上傳文件到服務(wù)器和客戶端
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)上傳文件到服務(wù)器和客戶端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

