微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Java方法内部创建对象

如何解决在Java方法内部创建对象

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;
            String array[i]=new array(name,address,email,roll);
    }
return array;}

是否可以在java方法内创建对象而不是在main方法中创建对象? &可以使用单个对象在数组中存储不同的数据吗?

解决方法

类似的事情可以解决问题。

我建议您使用Arraylist而不是array,但是我认为这是一些类工作,因此我不会对示例进行过多修改。

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 < 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;
            customers[i] = new Customer(name,address,email,roll);
        }
    }
}
,

是的,您可以使用Java中任何类型的方法创建对象。因此,根据您的代码片段,您正在尝试将具有一组属性的学生存储在数组中。要实现这一点,我认为最好的方法是使用ArrayList如果您不确定要注册的学生人数,但是如果您确切地知道人数[这意味着您可以指定数组大小],则可以使用数组。但是首先需要两种方式有一个包含学生属性的模型类。创建学生模型类后,您可以创建学生的数组或ArrayList类型。然后可以将学生详细信息存储在所选方法中。

例如:

步骤1:创建学生模型课程

 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,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
    }

第2步:ArrayList或数组实现

使用数组实现

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 < 10; 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();
                int roll = i + 1;
                studentsArr[i] = new Student(name,roll);
                System.out.println(i + 1 + " Student Registration complete");
                System.out.println("Your Rollnumber is " + (i + 1));
            }
        }
}


使用ArrayList进行实现

public class Form{

     public static void main(String[] args) {
            Scanner Input = new Scanner(System.in);
            //Creation of Student type array
            List<Student> studentsList = new ArrayList<>(); //Doesn'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("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();
                int roll = key + 1;
                studentsList.add(new Student(name,roll));//Adding the new student to list
                 System.out.println(key + 1 + " Student Registration complete");
                System.out.println("Your Rollnumber is " + (key + 1));
                System.out.println("press 0 to continue or -1 to end the process");
                end = input.nextInt();

            }while(end != -1);
      }
}


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。