[英]pass array to method Java
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?我该如何正确地做到这一点?
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 any other variable.只需将其作为任何其他变量传递即可。
In Java, arrays are passed by reference.在 Java 中,数组是通过引用传递的。
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.就这些。
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()
).如果您想要做的是创建数组的 COPY,则必须将数组传递到方法中,然后在那里手动创建副本(不确定 Java 是否有类似
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.否则,您将传递数组的 REFERENCE,因此如果您更改其中元素的任何值,其他方法也将更改该值。
In the method calling statement在方法调用语句中
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();
}
}
1 2 3 4 5 6 7 1 2 3 4 5 6 7
There is an important point of arrays that is often not taught or missed in java classes.数组有一个重要的点,在 Java 类中通常没有教授或遗漏。 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.
您可以使用这两个指针操作数组,但是一旦您将第二个指针分配给被调用方法中的新数组并通过 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
}
}
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 方法中应该以小写字母开头(这不是错误,而是约定)
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();
}
}
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
}
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.