通过对象传递输入时出错

时间:2014-04-03 10:11:23

标签: java

我创建了一个方法makesPerson(),它接受输入String []和int []。当我创建一个对象并将输入作为字符串数组和int时给出错误。当输入只是String和int而不是数组时,它正常工作。

public class Person {
    String name;
    int    height;
}


public class TestPerson {

    public void display(Person persons) {
        for (int i=0;i<persons.length;i++) {
            System.out.println(p.name[i]+":"+p.height[i]);
        }
    }

    public static void main(String args[]) {
        TestPerson p1=new TestPerson();

        //This is giving me error
        p1.makePerson({"joe" ,"jhon", "Alex"},{150,160,170});
    }

    public Person[] makePerson(String[] names, int[] hts) {
        Person [] persons = new Person(names.length);

        for(int i=0;i<names.length;i++) {
            Person p = new Person();

            p.name     = names[i];
            p.height   = hts[i];
            persons[i] = p;
        }

        String str = "";

        for(int i=0;i<persons.length;i++) {
            str += persons[i].height + ": :";
            System.out.println(str);
            display(persons);

            return persons;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

将方法参数更改为:

p1.makePerson(new String[]{"joe" ,"jhon", "Alex"}, new int[]{150,160,170});

而不是

p1.makePerson({"joe" ,"jhon", "Alex"},{150,160,170});// <- your sending String and ints not Arrays.

此外,您的显示方法是一群人而不是一个人。所以改成它:

public void display(Person[] persons) {

makePerson方法中,您以错误的方式声明数组,它应该是:

Person[] persons = new Person[names.length];

答案 1 :(得分:0)

嗯,你犯了几个错误,所以我重写了你的代码:

public class TestPerson{

    public void display(Person[] persons)
    {
        for (int i=0;i<persons.length;i++)
        System.out.println(persons[i].name+":"+persons[i].height);
    }

    public static void main(String args[]){
        TestPerson p1=new TestPerson();
        String[] names = {"Joe" ,"John", "Alex"};
        int[] hts = {150,160,170};
        Person[] persons = p1.makePerson(names,hts);

    }

    public Person[] makePerson(String[] names,int[] hts){
        Person [] persons = new Person[names.length];

        for(int i=0;i<names.length;i++){
            Person p= new Person();
            p.name=names[i];
            p.height=hts[i];
            persons[i]=p;
        }


        display(persons);

        return persons;
    }
    public class Person{
        String name;
        int height;

    }
}

现在,我相信,它符合您的预期。您的初始错误与方法调用期间错误的数组初始化有关。

输出:

Joe:150
John:160
Alex:170

答案 2 :(得分:-1)

您应该考虑在循环中添加一些安全检查。像:

public Person[] makePerson(String[] names, int[] hts) {
    if (names.length != hts.length)
        throw new IllegalArguementException("different array sizes");

    final Person[] persons = new Person[names.length];

    for(int i = 0; i < names.length; i++) {
        Person p = new Person();

        p.name     = names[i];
        p.height   = hts[i];
        persons[i] = p;
    }

    for(int i = 0; i < persons.length; i++) {
        System.out.println(persons[i].height + ": :");
        display(persons);
    }

    return persons;
}