将数组传递给方法 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1610757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pass array to method Java
提问by Dacto
How can I pass an entire array to a method?
如何将整个数组传递给方法?
private void PassArray() {
String[] arrayw = new String[4];
//populate array
PrintA(arrayw[]);
}
private void PrintA(String[] a) {
//do whatever with array here
}
How do I do this correctly?
我该如何正确地做到这一点?
采纳答案by NawaMan
You do this:
你做这个:
private void PassArray(){
String[] arrayw = new String[4]; //populate array
PrintA(arrayw);
}
private void PrintA(String[] a){
//do whatever with array here
}
Just pass it as other variable. In Java, Arrays are passed by reference.
只需将其作为其他变量传递即可。在 Java 中,数组是通过引用传递的。
回答by jjnguy
Simply remove the brackets from your original code.
只需从原始代码中删除括号即可。
PrintA(arryw);
private void PassArray(){
String[] arrayw = new String[4];
//populate array
PrintA(arrayw);
}
private void PrintA(String[] a){
//do whatever with array here
}
That is all.
就这些。
回答by Daniel T.
An array variable is simply a pointer, so you just pass it like so:
数组变量只是一个指针,因此您只需像这样传递它:
PrintA(arrayw);
Edit:
编辑:
A little more elaboration. If what you want to do is create a COPY of an array, you'll have to pass the array into the method and then manually create a copy there (not sure if Java has something like Array.CopyOf()
). Otherwise, you'll be passing around a REFERENCE of the array, so if you change any values of the elements in it, it will be changed for other methods as well.
再详细一点。如果您想要做的是创建数组的 COPY,则必须将数组传递给方法,然后在那里手动创建一个副本(不确定 Java 是否有类似Array.CopyOf()
)。否则,您将传递数组的 REFERENCE,因此如果您更改其中元素的任何值,其他方法也将更改该值。
回答by Bostone
You got a syntax wrong. Just pass in array's name. BTW - it's good idea to read some common formatting stuff too, for example in Java methods should start with lowercase letter (it's not an error it's convention)
你的语法错误。只需传入数组的名称。顺便说一句 - 阅读一些常见的格式化内容也是个好主意,例如在 Java 方法中应该以小写字母开头(这不是错误,而是约定)
回答by Arya Tanmay Gupta
Important Points
要点
- you have to use java.util package
- array can be passed by reference
- 你必须使用 java.util 包
- 数组可以通过引用传递
In the method calling statement
在方法调用语句中
- Don't use any object to pass an array
- only the array's name is used, don't use datatype or array brackets[]
- 不要使用任何对象来传递数组
- 只使用数组的名称,不要使用数据类型或数组括号[]
Sample Program
示例程序
import java.util.*;
class atg {
void a() {
int b[]={1,2,3,4,5,6,7};
c(b);
}
void c(int b[]) {
int e=b.length;
for(int f=0;f<e;f++) {
System.out.print(b[f]+" ");//Single Space
}
}
public static void main(String args[]) {
atg ob=new atg();
ob.a();
}
}
Output Sample Program
输出示例程序
1 2 3 4 5 6 7
1 2 3 4 5 6 7
回答by Anil Amane
class test
{
void passArr()
{
int arr1[]={1,2,3,4,5,6,7,8,9};
printArr(arr1);
}
void printArr(int[] arr2)
{
for(int i=0;i<arr2.length;i++)
{
System.out.println(arr2[i]+" ");
}
}
public static void main(String[] args)
{
test ob=new test();
ob.passArr();
}
}
回答by ytoamn
There is an important point of arrays that is often not taught or missed in java classes. When arrays are passed to a function, then another pointer is created to the same array ( the same pointer is never passed ). You can manipulate the array using both the pointers, but once you assign the second pointer to a new array in the called method and return back by void to calling function, then the original pointer still remains unchanged.
数组有一个重要的点,在 Java 类中通常没有教授或遗漏。当数组传递给函数时,会创建另一个指向同一个数组的指针(永远不会传递同一个指针)。您可以使用这两个指针操作数组,但是一旦您将第二个指针分配给被调用方法中的新数组并通过 void 返回到调用函数,则原始指针仍然保持不变。
You can directly run the code here : https://www.compilejava.net/
你可以在这里直接运行代码:https: //www.compilejava.net/
import java.util.Arrays;
public class HelloWorld
{
public static void main(String[] args)
{
int Main_Array[] = {20,19,18,4,16,15,14,4,12,11,9};
Demo1.Demo1(Main_Array);
// THE POINTER Main_Array IS NOT PASSED TO Demo1
// A DIFFERENT POINTER TO THE SAME LOCATION OF Main_Array IS PASSED TO Demo1
System.out.println("Main_Array = "+Arrays.toString(Main_Array));
// outputs : Main_Array = [20, 19, 18, 4, 16, 15, 14, 4, 12, 11, 9]
// Since Main_Array points to the original location,
// I cannot access the results of Demo1 , Demo2 when they are void.
// I can use array clone method in Demo1 to get the required result,
// but it would be faster if Demo1 returned the result to main
}
}
public class Demo1
{
public static void Demo1(int A[])
{
int B[] = new int[A.length];
System.out.println("B = "+Arrays.toString(B)); // output : B = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Demo2.Demo2(A,B);
System.out.println("B = "+Arrays.toString(B)); // output : B = [9999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println("A = "+Arrays.toString(A)); // output : A = [20, 19, 18, 4, 16, 15, 14, 4, 12, 11, 9]
A = B;
// A was pointing to location of Main_Array, now it points to location of B
// Main_Array pointer still keeps pointing to the original location in void main
System.out.println("A = "+Arrays.toString(A)); // output : A = [9999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
// Hence to access this result from main, I have to return it to main
}
}
public class Demo2
{
public static void Demo2(int AAA[],int BBB[])
{
BBB[0] = 9999;
// BBB points to the same location as B in Demo1, so whatever I do
// with BBB, I am manipulating the location. Since B points to the
// same location, I can access the results from B
}
}
回答by Sunil kumawat
public static void main(String[] args) {
int[] A=new int[size];
//code for take input in array
int[] C=sorting(A); //pass array via method
//and then print array
}
public static int[] sorting(int[] a) {
//code for work with array
return a; //retuen array
}
回答by akay
In this way we can pass an array to a function, here this print function will print the contents of the array.
这样我们就可以将一个数组传递给一个函数,这里这个打印函数将打印数组的内容。
public class PassArrayToFunc {
public static void print(char [] arr) {
for(int i = 0 ; i<arr.length;i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char [] array = scan.next().toCharArray();
print(array);
scan.close();
}
}