在java中返回一个对象数组 [英] returning an array of object in java

查看:75
本文介绍了在java中返回一个对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在BtechStudent类中创建一个名为registration的方法,我必须在一个对象数组中读取并存储学生的数据并返回。

最后我必须打印所有数据来自对象阵列的学生。

这里是代码............



I have to create a method called registration in BtechStudent class and i have to read and store the data of students in an array of object and return.
finally i have to print the data of all students from array of object.
here is the code............

import java.io.*;
class BtechStudent
{
    void registration()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter name");
        String name=br.readLine();
        System.out.println("branch");
        String branch=br.readLine();
        System.out.println("idNumber");
        String idNumber=br.readLine();
        //HOW TO RETURN THE OBJECT?????
    }
}

class DataBase
{
    public static void main(String args[])throws IOException
    {
        BtechStudent b[]=new BtechStudent[3];//I HAVE TAKEN 3 STUDENTS DATA..
        String name;
        String branch;
        String idNumber;
        for(int i=0;i<3;i++)
        {
            b[i].registration();
        }
        for(int j=0;j<3;j++)
        {
            System.out.println(b[j]);
        }
    }
}
//I WANT TO ENTER THE DATA OF STUDENT FROM registration METHOD NOT IN MAIN METHOD...AND //PRINT THE DETAILS OF STUDENTS MAIN METHOD....
//PLEASE HELP ME HOW TO DO.........

推荐答案

你似乎是一名学生,你似乎对数组如何有一些误解。因此,我会尽力解释。



首先,在 main 方法中你创建一个 BtechStudent 对象的数组。创建数组时,指定大小(在本例中,您使用 3 )。这为3 BtechStudent 对象创建了占位符。它不会创建对象。在循环中,当你执行 b [i] .registration(); 时,没有对象可以调用注册方法。要解决此问题,您需要创建一个对象并将其放在数组中,然后再尝试调用 registration 方法。因此,这会将您的循环更改为:

You seem to be a student, and you seem to have some misconceptions about how arrays and so forth. As such, I'll try to explain as best I can.

First off, in your main method you create an array of BtechStudent objects. When you create a array, you specify the size (in this case, you used 3). This creates "placeholders" for 3 BtechStudent objects. It does not create the objects. in your loop, when you do b[i].registration();, there is no object on which to invoke the registration method. To fix this problem, you need to create an object and place it in the array before you try to call the registration method. So, that changes your loop to:
for(int i=0;i<3;i++)
{
    b[i] = new BtechStudent();
    b[i].registration();
}





其次,当您创建注册方法时,你指定它将返回一个 BtechStudent 对象,但它永远不会返回任何内容。在方法结束时,您需要有一个 return 语句。由于您从 BtechStudent 类返回 BtechStudent 对象,因此您可能希望使用返回此; 作为返回语句。 是一个特殊关键字,指的是您当前正在执行方法的对象。



表示,你可能不应该在该方法中返回 BtechStudent 对象,因为它会是多余的。除非您有理由想要返回 BtechStudent 对象,否则我建议您将方法声明更改为



Secondly, when you create your registration method, you specify that it will return a BtechStudent object, but it never returns anything. At the end of the method, you need to have a return statement. Since you are returning a BtechStudent object from the BtechStudent class, you will probably want to use return this; as your return statement. this is a special keyword that refers to the object on which you are currently executing a method.

That said, you should probably not return a BtechStudent object in that method anyway, since it would be redundant. Unless you have some reason for wanting to return a BtechStudent object, I suggest you change the method declaration to

void registration() throws IOException



void 表示该方法不返回任何内容,因此您可以调用它来做某些工作或以某种方式更改对象。



接下来是全局变量。在 registration 方法中,您可以捕获一些详细信息并将其存储在本地变量中。这意味着,一旦方法结束,就不再可以访问变量。从本质上讲,它意味着您要求用户捕获数据,然后将数据丢弃。而是在 BtechStudent 类中创建一些全局变量,并将捕获的数据存储在其中。这可确保只要您拥有该对象,就可以获得用户捕获的数据。到目前为止,您的 BtechStudent 类看起来像这样:


void means that the method does not return anything, so you can just call it to do some work or change the object in some way.

Next up is global variables. In your registration method, you capture some details and store it in local variables. This means that, as soon as the method ends, the variables are no longer accessible. Essentially, it means you asked the user to capture data and then you threw that data away. Instead, create some global variables in the BtechStudent class and store the captured data in them. This ensures that as long as you have the object, you have the data that the user captured. By now, your BtechStudent class looks like this:

class BtechStudent
{
    public String name;
    public String branch;
    public String idNumber;

    void registration() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter name");
        name=br.readLine();
        System.out.println("branch");
        branch=br.readLine();
        System.out.println("idNumber");
        idNumber=br.readLine();
    }
}





最后,在你的 main 方法,您尝试打印 BtechStudent 对象。问题是,它是你创建的一个类,所以Java不知道如何打印它。这将导致Java打印一些无用的废话,而不是您希望看到的名称,分支和ID号。为了告诉Java在您尝试打印对象时要打印什么,您需要在 BtechStudent toString()方法c $ c>上课。它看起来应该是这样的:



Lastly, in your main method, you try to print the BtechStudent object. The problem is, it's a class you created, so Java has no idea how to print it. That will result in Java printing some useless crap instead of the name, branch and ID number you expect to see. In order to tell Java what to print when you try to print the object, you need to create a toString() method in your BtechStudent class. It should look something like this:

public string toString()
{
    return "Name='" + name + "', Branch='" + branch + "', ID Number='" + idNumber + "'.";
}


Hello Vjnan,



以下代码片段可以帮助您入门。

Hello Vjnan,

The following code snippet should get you started.
public class BTechStudent {
    private String _strName;
    private String _strBranch;
    private String _strId;

    public BTechStudent() {
    }

    public BTechStudent(String strName, String strBranch, String strId) {
        _strName = strName;
        _strBanch = strBranch;
        _strId = strId
    }

    public String getStudentName() {
        return _strName;
    }

    public String getBranch() {
        return _strBranch;
    }

    public String getId() {
        return _strId;
    }

    public void setStudentName(String pstrName) {
        _strName = pstrName;
    }
 
   public void setBranch(String pstrBranch) {
        _strBranch = pstrBranch;
    }
    public void setId(String pstrId) {
        _strId = pstrId;
    }
}

public class TestClass {
    public BTechStudent[] readStudentInfo() throws IOException {
        String strName = null;
        String strBranch = null;
        String strId = null;
        BTechStudent student = null;
        BufferedReader rdr = null;
        BTechStudent[] arrStudent = null;
 
        try {
            rdr = new BufferedReader(new InputStreamReader(System.in));
            arrStudent = new BTechStudent[3];
            for (int i = 0; i < 3; i++) {
                System.out.println("enter name");
                strName = rdr.readLine();
                System.out.println("branch");
                strBranch = br.readLine();
                System.out.println("idNumber");
                strId = br.readLine();
                student = new BTechStudent(strName, strBranch, strId);
                arrSrudent[i] = student;
            }
            return arrStudent;
        } finally {
            CleanUpUtils.doClose(rdr);
        }
    }

    public static void main(String[] args) throws IOException {
        TestClass tst = null;
        BTechStudent[] arrRet = null;

        tst = new TestClass();
        arrRet = tst.readStudentInfo();
        for (BTechStudent student : arrRet) {
             System.out.println("Student Name : " + student.getStudentName());
             System.out.println("Branch       : " + student.getBranch());
             System.out.println("Student Id   : " + student.getId());
             System.out.println("");
        }
    }
}

public abstract class CleanUpUtils {
    public static doClose(ICloseable toClose) {
        if (null == toClose) return;
        try {
            toClose.close();
        } catch (IOException ex) {
           // do nothing
        }
    }
}



问候,


Regards,


这篇关于在java中返回一个对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆