Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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,我只想创建一个对象,我不确定我是否正在创建,它是否正确。我的示例是,假设我有2个类(Userinput和paper)。0f原因是和main。在这个示例中,没有继承(只有2个简单类)。我创建的对象正确吗?我如何将其放入数组或同一数组中 package exercise; public class Exercise{ static int N; //from keyboard .I have a class userinput.It doesnt need to write it here

我只想创建一个对象,我不确定我是否正在创建,它是否正确。我的示例是,假设我有2个类(Userinput和paper)。0f原因是和main。在这个示例中,没有继承(只有2个简单类)。我创建的对象正确吗?我如何将其放入数组或同一数组中

package exercise;
public class Exercise{
    static int N; //from keyboard .I have a class userinput.It doesnt need to write it here ,i have in the other class the problem

    public static void main(String[] args) { // main class

        Paper[] pin = new Paper[N]; //i create an array
        Paper.setpencil(3); // i wrote the 3 .In this way i create 3 pencil?
        Paper.getpencil(3); 

        Paper.setsomething(4); // i wrote the 4 .I create 4 ?
        Paper.getsomething(4); 

    } }
public class Paper{ //in this class i am confused
    public Paper(){} //default constructor 

    private int pencil;
    private String something; 

    public int getpencil(){
        return pencil;
    } 
    public void setpencil(){
        pencil=UserInput.getInteger():
    }
    public int getsomething(){
        return something;
    }
    public void setsomething(){
        something=UserInput.setInteger();
    }
}  
谨此声明:

Paper[] pin = new Paper[N];
创建类纸张对象的数组

还必须为每个数组元素创建一个对象,如:

for (int i=0; i < N, i++)
{
    pin[i] = new Paper();
}

让代码可编译是该过程的第一步。实际上,由于很多原因,这段代码无法编译。请提供一个正确格式的。您现在展示的代码不应该编译,因为
setpencil
等都是实例方法,但您在
纸上调用它们,就好像它们是静态方法一样。另外你
Paper
的构造函数被称为
Paper
,因此它也不会编译。这些都与数组无关。我建议一次处理一件事。可能的重复,只是类型不同。在这种情况下,您的代码有很多错误,包括编译时错误,我建议使用像Eclipse这样的IDE,这将对您有很大帮助。如果您要求人们尝试阅读,请正确缩进您的代码。
pin[0].setpencil(3);