Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,将对象数组从一个方法传递到另一个方法_Java_Arrays_Object_Loops_For Loop - Fatal编程技术网

在java中,将对象数组从一个方法传递到另一个方法

在java中,将对象数组从一个方法传递到另一个方法,java,arrays,object,loops,for-loop,Java,Arrays,Object,Loops,For Loop,我想将存储在一个for循环中的对象数组传递给另一个方法中的第二个for循环,以显示内容。e、 g: public static Student[] add() for(int i = 0; i < studentArray.length; i++) { System.out.print("Enter student name "); studentName = EasyIn.getString(); S

我想将存储在一个for循环中的对象数组传递给另一个方法中的第二个for循环,以显示内容。e、 g:

public static Student[] add()
for(int i = 0; i < studentArray.length; i++)
        {
            System.out.print("Enter student name ");
            studentName = EasyIn.getString();
            System.out.print("Enter student Id ");
            studentId = EasyIn.getString();
            System.out.print("Enter mark ");
            studentMark = EasyIn.getInt();
            studentArray[i] = new Student(); //create object
            tempObject = new Student(studentName,studentId,studentMark);
            place = findPlace(studentArray,studentName, noOfElements);
            noOfElements = addOne(studentArray, place, tempObject, noOfElements);   
        }
当我按菜单上的3键时,它只会再次调用add方法,但当我再次将值添加到数组中时,它会显示数组。我只想只显示数组

更改displayAll()以将数组作为参数:

public void displayAll(Student[] students) {
  ...
}
并称之为:

Student [] students = add();

...

case '3': System.out.println("List All");
    displayAll(students);
    EasyIn.pause();
更改displayAll()以将数组作为参数:

public void displayAll(Student[] students) {
  ...
}
并称之为:

Student [] students = add();

...

case '3': System.out.println("List All");
    displayAll(students);
    EasyIn.pause();

您可以将displayAll方法定义更改为

public static void displayAll(Student[] anotherArray)
{    
    for(int i = 0; i < anotherArray.length ; i++)
    {
        System.out.print(anotherArray[i].toString());
    }   
}
publicstaticvoiddisplayall(Student[]另一个数组)
{    
for(int i=0;i

然后,在切换case之前,从任何要调用的位置调用add方法,并使用student[]作为参数调用displayAll方法。

您可以将displayAll方法定义更改为

public static void displayAll(Student[] anotherArray)
{    
    for(int i = 0; i < anotherArray.length ; i++)
    {
        System.out.print(anotherArray[i].toString());
    }   
}
publicstaticvoiddisplayall(Student[]另一个数组)
{    
for(int i=0;i
然后,在切换case之前从任何您想调用的地方调用add方法,并调用displayAll方法,其中student[]作为参数。

与其他方法类似

public void displayAll(Student... students) {    
    for(Student student:students)
        System.out.print(student+" "); // so there is space between the toString
    System.out.println();
}

相似

public void displayAll(Student... students) {    
    for(Student student:students)
        System.out.print(student+" "); // so there is space between the toString
    System.out.println();
}


你的代码无法编译。在你的
add()
方法中,你应该把
{
}
放在大括号里,并且它应该
返回
学生[]
类型的一些内容。问题不清楚,你能澄清你不想要的行为吗?这是因为每次调用
add()
时,您都在重建数组,而您只希望这种情况发生一次?它们只是我所展示的代码的一个样本,我是否应该粘贴我所做的所有代码并展示给您?@john B每次调用display方法时,它都会重新构建数组,然后显示它。我只想显示它。如果
add()
重建数组,并且您在
displayAll()
的开头调用
add()
(在它内部),每次调用
displayAll()
,您都将重建数组。尝试@mcfinnigan或@Rahul Borkar solutions。您的代码将无法编译。在你的
add()
方法中,你应该把
{
}
放在大括号里,并且它应该
返回
学生[]
类型的一些内容。问题不清楚,你能澄清你不想要的行为吗?这是因为每次调用
add()
时,您都在重建数组,而您只希望这种情况发生一次?它们只是我所展示的代码的一个样本,我是否应该粘贴我所做的所有代码并展示给您?@john B每次调用display方法时,它都会重新构建数组,然后显示它。我只想显示它。如果
add()
重建数组,并且您在
displayAll()
的开头调用
add()
(在它内部),每次调用
displayAll()
,您都将重建数组。试试@mcfinnigan或@Rahul Borkar solutions。它在一定程度上起作用,每次调用Student[]students=add();它调用add()方法的原因很奇怪,每次调用Student[]students=add(),它都会在一定程度上工作;出于某种奇怪的原因,它调用add()方法