2

I have simply this below class structure and I want to add any item to it.

public class Person implements Serializable {
    private String name;
    private String mobile;

    public Person(String n, String e) { name = n; mobile = e; }

    public String getName() { return name; }
    public String getMobile() { return mobile; }

    @Override
    public String toString() { return name; }
}

I want to add any item like with this:

    people = new Person[]{
            new Person("Hi"   , " programmer"),
            new Person("Hello", " world")
    };

My code is this and I want to add items into that by while() my code is don't correct.

people =  new Person[]{};
while (phones.moveToNext())
{
        people =  new Person("Hi"   , " programmer");
        people =  new Person("Hello", " world")
}
4
  • 1
    What problem are you having? What is phones.moveToNext()? Commented Nov 11, 2014 at 9:10
  • 2
    You're assigning Person object reference to an Array Commented Nov 11, 2014 at 9:12
  • Why not use a a List? Since you don't know how many people you're going to have... Commented Nov 11, 2014 at 9:13
  • 1
    It seems like you want a auto-expandable, or resizable array. That doesn't exist. What you probably want is an ArrayList. But you should definitely understand arrays first. docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html. Commented Nov 11, 2014 at 9:14

5 Answers 5

1

you have error in your source code you are trying to put Object of person into array so it will gives you compilation error to overcome this problem first take List of Type Person and convert it into array and do your business logic on Array its better to use List instead of Array

           List<Person> personlst = new ArrayList<Person>();
            while (phones.moveToNext())
            {
                personlst.add(new Person("Hi"   , " programmer"));
                personlst.add(new Person("Hello", " world"));
            }
            Object[]arryPer = personlst.toArray();
            Person[]people = new Person[arryPer.length];

        for (int j = 0; j < arryPer.length; j++) {
            people[j] = (Person) arryPer[j];
        }

above code of block give you array of type people

Sign up to request clarification or add additional context in comments.

2 Comments

thanks sir. i get this erro : FATAL EXCEPTION: main java.lang.ClassCastException: java.lang.Object[] cannot be cast to ir.tsms.Person[] for this line Person[]people = (Person[]) arryPer
@andbee: it has some casting problem.check edited one
0

You are not defining the number of elements that you want to push into array. Also you are not even making those elements to an array. You should do something like:

int i =0;
people =  new Person[1000];// you need to define how many elements you need here or go for list
while (phones.moveToNext())
{
    people[i++] =  new Person("Hi"   , " programmer");
    people[i++] =  new Person("Hello", " world")
}

Comments

0

Define first the size of your Person.

Person[] people = new Person[10];

then do your iteration for example.

for(int i = 0; i < 0; i++){
            people[i] = new Person("Hi"   , " programmer");
}

Comments

0

first put all elements in list then form the array:

List<Person> list = new ArrayList<Person>();
    while (phones.moveToNext()) {
        list.add(new Person("Hi", " programmer"));
        list.add(new Person("Hello", " world"));
    }
    Person[]  persons = new Person[list.size()];
    list.toArray(persons);
}

Comments

0

Try Arrays.asList

http://www.tutorialspoint.com/java/util/arrays_aslist.html

Note - it is good only for a small number of elements as arrays generally take contiguous memory.

As people have stated above, list is any day better from space point of view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.