Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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 - Fatal编程技术网

Java—如何生成类的对象数组并调用具有两个参数的构造函数

Java—如何生成类的对象数组并调用具有两个参数的构造函数,java,Java,我想知道我是否可以在java中创建一个类的对象数组,这样就可以使用两个或多个参数调用构造函数。 例如,看看我的代码- 主要类别为: package staticfunction; import java.util.Scanner; public class MainClass { public static void main(String args[]){ String name; int marks; int ch; Scanner input = n

我想知道我是否可以在java中创建一个类的对象数组,这样就可以使用两个或多个参数调用构造函数。 例如,看看我的代码-

主要类别为:

package staticfunction;
import java.util.Scanner;

public class MainClass {

public static void main(String args[]){

    String name;
    int marks;
    int ch;

    Scanner input = new Scanner(System.in);
    StaticClass[] object = new StaticClass[10];  //Array of objects initalisation

    System.out.print("How many students : ");
    ch = input.nextInt();

    for(int i=0; i<ch; i++){
        System.out.print("\nEnter the name of student : ");
        name = input.nextLine();
        System.out.printf("\nEnter marks of %s", name);
        marks = input.nextInt();

        /*What should be the code here so that I can invoke a parametrized constructor*/

    }       
}

}

首先。您将类命名为
StaticClass
。如果要使这个类中的所有方法都是静态的,那么创建任何对象都没有用。而且您也将无法使用您可能想要的
count
变量

要在数组中创建和添加类的实例,只需添加以下内容:

for(int i=0; i<ch; i++){
    System.out.print("\nEnter the name of student : ");
    name = input.nextLine();
    System.out.printf("\nEnter marks of %s", name);
    marks = input.nextInt();

    object[i] = new StaticClass(name, marks);

}   

为了在java中创建一个数组,您需要在声明它之前准确地知道您有多少个元素。这是因为数组是一个连续内存块,一旦分配了它,以后就不能再添加。 一种(不好的)方法是声明一个非常大的数组并只部分填充它

或者,您可以放弃在程序开始时声明StaticClass对象数组,而只在用户向您提供他们希望提供的条目数后才声明它

不过,现在正是使用ArrayList的好时机,ArrayList是一种容器类型,允许您根据需要对其进行扩展和收缩

您可以使用以下方法:

package staticfunction;
import java.util.*; //imports ArrayList

public class MainClass {

public static void main(String args[]){

    String name;
    int marks;
    int ch;

    Scanner input = new Scanner(System.in);

    System.out.print("How many students : ");
    ch = input.nextInt();

    ArrayList myArrayList = new ArrayList(); //create your container.

        for(int i=0; i<ch; i++){
            System.out.print("\nEnter the name of student : ");
            name = input.nextLine();
            System.out.printf("\nEnter marks of %s", name);
            marks = input.nextInt();

            myArrayList.add(new StaticClass(name,marks)); //make a new StaticClass instance and push it to your container;
        }       

        //your ArrayList now holds the objects.

    // you can get the first element:
    StaticClass firstElement= myArrayList.get(0);

    //you can get the last element:
    StaticClass lastElement= myArrayList.get(myArrayList.size()-1);


    //iterate through all elements:
   for(int i=0; i<myArrayList.size(); i++)
   {
        StaticClass current=myArrayList.get(i);
        //do something with current here
   }

}

}
包静态功能;
导入java.util.*//导入ArrayList
公共类主类{
公共静态void main(字符串参数[]){
字符串名;
整数标记;
int-ch;
扫描仪输入=新扫描仪(System.in);
系统输出打印(“多少学生:”;
ch=input.nextInt();
ArrayList myArrayList=新建ArrayList();//创建容器。

对于(int i=0;iWORD是C++相关的)?你问如何实例化类?<代码>新StaticClass(名字,标记);
将结果分配给相应的数组元素。我想问的是,如何在对象数组的帮助下调用具有两个参数的StaticClass构造函数。该数组与
StaticClass
的实例化无关。这意味着我必须在StaticClass中创建一个seprate函数?
System.out.print("How many students : ");
ch = input.nextInt();
StaticClass[] object = new StaticClass[ch];  //Array of objects initalisation
package staticfunction;
import java.util.*; //imports ArrayList

public class MainClass {

public static void main(String args[]){

    String name;
    int marks;
    int ch;

    Scanner input = new Scanner(System.in);

    System.out.print("How many students : ");
    ch = input.nextInt();

    ArrayList myArrayList = new ArrayList(); //create your container.

        for(int i=0; i<ch; i++){
            System.out.print("\nEnter the name of student : ");
            name = input.nextLine();
            System.out.printf("\nEnter marks of %s", name);
            marks = input.nextInt();

            myArrayList.add(new StaticClass(name,marks)); //make a new StaticClass instance and push it to your container;
        }       

        //your ArrayList now holds the objects.

    // you can get the first element:
    StaticClass firstElement= myArrayList.get(0);

    //you can get the last element:
    StaticClass lastElement= myArrayList.get(myArrayList.size()-1);


    //iterate through all elements:
   for(int i=0; i<myArrayList.size(); i++)
   {
        StaticClass current=myArrayList.get(i);
        //do something with current here
   }

}

}