在Java方法内部创建对象

huangapple go评论68阅读模式
英文:

Creating Object Inside Java Method

问题

public static String[] Form() {
    Scanner Input = new Scanner(System.in);
    String[] array;
    array = new String[2];
    RegistrationForm RegisteredStudents = new RegistrationForm();
    for (int i = 0; i < 2; i++) {
        System.out.println("Enter Name");
        String name = Input.next();
        System.out.println("Enter Address");
        String address = Input.next();
        System.out.println("Enter E-Mail");
        String email = Input.next();
        System.out.println(i + 1 + " Student Registration complete");
        System.out.println("Your Rollnumber is " + (i + 1));
        int roll = i + 1;
        array[i] = new array(name, address, email, roll);
    }
    return array;
}
is it possible to create an object inside a java method instead of creating it in main method? & can a single object be used to store different data in an array?
英文:
public static String[] Form() {
        Scanner Input=new Scanner(System.in); 
        String array[];
        array = new String[2];
        RegistrationForm RegisteredStudents = new RegistrationForm();
        for(int i=0; i&lt;2; i++){
            System.out.println(&quot;Enter Name&quot;);
            String name= Input.next();
            System.out.println(&quot;Enter Address&quot;);
            String address= Input.next();
            System.out.println(&quot;Enter E-Mail&quot;);
            String email= Input.next();
            System.out.println(i+1+&quot; Student Registration complete&quot;);
            System.out.println(&quot;Your Rollnumber is &quot;+(i+1));
            int roll=i+1;
            String array[i]=new array(name,address,email,roll);
    }
return array;}

is it possible to create an object inside a java method instead of creating it in main method? & can a single object be used to store different data in an array?

答案1

得分: 1

像这样的代码可以完成任务。

我建议您使用ArrayList而不是数组,但我假设这是一些课程作业,所以我不会修改您的示例太多。

import java.util.Scanner;
import java.util.ArrayList;

class Customer {

    private String name;
    private String address;
    private String email;
    private int roll;

    public Customer(String name, String address, String email, int roll) {
        this.name = name;
        this.address = address;
        this.email = email;
        this.roll = roll;
    }

    public int getRoll() {
        return roll;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public String getEmail() {
        return email;
    }
}

class Scratch {
    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);
        ArrayList<Customer> customers = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            System.out.println("输入姓名");
            String name = Input.next();
            System.out.println("输入地址");
            String address = Input.next();
            System.out.println("输入电子邮件");
            String email = Input.next();
            System.out.println(i + 1 + " 学生注册完成");
            System.out.println("您的Roll号码是 " + (i + 1));
            int roll = i + 1;
            customers.add(new Customer(name, address, email, roll));
        }
    }
}
英文:

Something like this would do the trick.

I recommend you to use Arraylist instead of array, but I presume it's some class work so I don't modify too much your example.

import java.util.Scanner;
class Customer {
private String name;
private String address;
private String email;
private int roll;
public Customer(String name, String address, String email, int roll) {
this.name = name;
this.address = address;
this.email = email;
this.roll = roll;
}
public int getRoll() {
return roll;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getEmail() {
return email;
}
}
class Scratch {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
Customer customers[] = new Customer[10];
for (int i = 0; i &lt; 2; i++) {
System.out.println(&quot;Enter Name&quot;);
String name = Input.next();
System.out.println(&quot;Enter Address&quot;);
String address = Input.next();
System.out.println(&quot;Enter E-Mail&quot;);
String email = Input.next();
System.out.println(i + 1 + &quot; Student Registration complete&quot;);
System.out.println(&quot;Your Rollnumber is &quot; + (i + 1));
int roll = i + 1;
customers[i] = new Customer(name, address, email, roll);
}
}
}

答案2

得分: 1

是的,在Java中你可以在任何类型的方法中创建对象。根据你的代码片段,你正在尝试在数组中存储一组具有属性的学生对象。为了实现这一点,我认为最好的方法是在不确定要注册的学生数量的情况下使用ArrayList,但如果你确切地知道数量(也就是说,如果你可以指定数组大小),你可以使用数组。无论哪种方式,首先你需要有一个包含学生属性的模型类。创建了学生模型类之后,你可以创建一个数组或ArrayList类型的学生对象,然后可以在所选的方法中存储学生详细信息。

步骤1:创建学生模型类

 public class Student {
         
        //成员变量
        private String name;
        private String address;
        private String email;
        private int roll;
    
        //默认构造函数
        public Student(){}

       //重载构造函数
        public Student(String name, String address, String email, int roll) {
            this.name = name;
            this.address = address;
            this.email = email;
            this.roll = roll;
        }

        //获取器和设置器
        public void setRoll(int roll){
             this.roll = roll;
        }

        public int getRoll() {
            return roll;
        }
    
        public void setName(String name){
            this.name =name;   
        }
        public String getName() {
            return name;
        }
        public void setAddress(String address){
            this.address = address;
        }
       
        public String getAddress() {
            return address;
        }
         public void setEmail(String email){
            this.email = email;
         }
        public String getEmail() {
            return email;
        }

    //最好也在这里实现toString()方法
}

步骤2:ArrayList或数组实现

使用数组实现

public class Form{

     public static void main(String[] args) {
            Scanner Input = new Scanner(System.in);
            //创建学生类型的数组
            Student studentsArr[] = new Studnet[10]; //假设只有10名学生要注册
            for (int i = 0; i < 10; i++) {
                System.out.println("输入姓名");
                String name = Input.next();
                System.out.println("输入地址");
                String address = Input.next();
                System.out.println("输入电子邮件");
                String email = Input.next();
                int roll = i + 1;
                studentsArr[i] = new Student(name, address, email, roll);
                System.out.println(i + 1 + "名学生注册完成");
                System.out.println("你的学号是 " + (i + 1));
            }
        }
}

使用ArrayList实现

public class Form{

     public static void main(String[] args) {
            Scanner Input = new Scanner(System.in);
            //创建学生类型的ArrayList
            List<Student> studentsList = new ArrayList<>(); //不知道学生数量,因此使用列表
            int end = 0; //终止条件的初始值
            int key = -1;
            do {
                key++;
                System.out.println("输入姓名");
                String name = Input.next();
                System.out.println("输入地址");
                String address = Input.next();
                System.out.println("输入电子邮件");
                String email = Input.next();
                int roll = key + 1;
                studentsList.add(new Student(name, address, email, roll)); //将新学生添加到列表中
                System.out.println(key + 1 + "名学生注册完成");
                System.out.println("你的学号是 " + (key + 1));
                System.out.println("按0继续或按-1结束进程");
                end = input.nextInt();

            } while (end != -1);
      }
}
英文:

Yes you can create objects in any type of method in Java.So according to your code snippet ,you are trying to store a student with set of attributes in an array.To achieve this I think the best way is using an ArrayList if you are not sure about the students count that you are going to register,but if you exactly know the count[This means if you can specify the array size] you can go with an array.But in the both ways first you need to have a model class including the student attributes.After creating the Student model class you can create an array or an ArrayList type of Student.Then you can store your student details in the selected method.

Ex:

Step 1:Creating the Student model class

 public class Student {
//Member variables
private String name;
private String address;
private String email;
private int roll;
//Default constructor
public Student(){}
//Overloaded constructor
public Student(String name, String address, String email, int roll) {
this.name = name;
this.address = address;
this.email = email;
this.roll = roll;
}
//Getters and Setters
public void setRoll(int roll){
this.roll = roll;
}
public int getRoll() {
return roll;
}
public void setName(String name){
this.name =name;   
}
public String getName() {
return name;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress() {
return address;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail() {
return email;
}
//Better if you can also implement the toString() method in here
}

Step 2:ArrayList or array implementation

Implementing by using an Array

public class Form{
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
//Creation of Student type array
Student studentsArr[] = new Studnet[10]; //Considering there are only 10 Students to register
for (int i = 0; i &lt; 10; i++) {
System.out.println(&quot;Enter Name&quot;);
String name = Input.next();
System.out.println(&quot;Enter Address&quot;);
String address = Input.next();
System.out.println(&quot;Enter E-Mail&quot;);
String email = Input.next();
int roll = i + 1;
studentsArr[i] = new Student(name, address, email, roll);
System.out.println(i + 1 + &quot; Student Registration complete&quot;);
System.out.println(&quot;Your Rollnumber is &quot; + (i + 1));
}
}
}

Implementing by using an ArrayList

public class Form{
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
//Creation of Student type array
List&lt;Student&gt; studentsList = new ArrayList&lt;&gt;(); //Doesn&#39;t know the students count ,because of that we are using a list
int end = 0;//Terminating condition initial value
int key = -1;
do{
key++;
System.out.println(&quot;Enter Name&quot;);
String name = Input.next();
System.out.println(&quot;Enter Address&quot;);
String address = Input.next();
System.out.println(&quot;Enter E-Mail&quot;);
String email = Input.next();
int roll = key + 1;
studentsList.add(new Student(name, address, email, 
roll));//Adding the new student to list
System.out.println(key + 1 + &quot; Student Registration complete&quot;);
System.out.println(&quot;Your Rollnumber is &quot; + (key + 1));
System.out.println(&quot;press 0 to continue or -1 to end the process&quot;);
end = input.nextInt();
}while(end != -1);
}
}

huangapple
  • 本文由 发表于 2020年10月1日 16:00:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64151258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定