java 数组参数_java中 数组可以作为形式参数传递到调用的方法中吗?要怎么操作?...
展開全部
可以,操作代碼如下:
public class ArrTest{
public static void doTest(String[] args){
for(int i=0;i
System.out.println(args[i]);
}
}
public static void main(String[] args){
String[] testArr = {"a","b","c","d"};
ArrTest.doTest(testArr );
}
}
擴展資料:
把數組作為參數傳入一個方法,在該方法中對數e69da5e887aa3231313335323631343130323136353331333366306464組進行一些操作:如果僅僅是插入數據項等一般操作,那么該操作會影響到數組本身;反之,如調整數組大小、對數組賦值等操作,則對數組本身沒有影響。
例子:
public class Tester {
public static void counter(int count) {
count = 2;
}
public static void changeA1(int[] ints) {
int[] temp = { 4, 5, 6 };
ints = temp;
}
public static void changeA2(int[] ints) {
ints[0] = 4;
ints[1] = 5;
ints[2] = 6;
}
public static void main(String[] args) {
// Output: 1
// 基本數據類型沒有改變。
int count = 1;
counter(count);
System.out.println("count: " + count);
int[] ints = { 1, 2, 3 };
// Output: 1, 2, 3
// 對數組賦值,不會改變原始數組。
changeA1(ints);
for (int i = 0; i < ints.length; i++) {
System.out.print(ints[i] + " ");
}
// Output: 4, 5, 6
// 可以對數組插入新的數據項。
System.out.println();
changeA2(ints);
for (int i = 0; i < ints.length; i++) {
System.out.print(ints[i] + " ");
}
}
}
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java 数组参数_java中 数组可以作为形式参数传递到调用的方法中吗?要怎么操作?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql数据回滚占用id吗_mysql
- 下一篇: java commons-chain_A