Lox
Lox

Reputation: 299

How to create and display a list in Java?

i have to create a list of ,let's say 50 people, (in Java) and display the list, and i don't really know how to do that. so this is what i have tried to do so far . please correct and complete some of my code .

public class Person {

    String name;
    String stuff;
}


public class CreatePerson {



public static void  ang()  {    

    ArrayList<Person> thing=new ArrayList<Person>(); 
    Scanner diskScanner = new Scanner(in);


    for(int i=0; i<50; i++){

        Person pers = new Person();

          out.print("name: ");
      pers.name=diskScanner.nextLine();

      out.print("stuff: ");
      pers.stuff=diskScanner.nextLine();

      thing.add(pers);

          break;

    }
    // Display people  
    for (int i=0; i<50; i++) {
        out.println(??);{
        }

} }}

Upvotes: 0

Views: 8627

Answers (6)

Javier
Javier

Reputation: 1

I have a better upgrade to your class in order to be better. You can convert the class Person in a JavaBean class adding the methods setName(String name) and setStuff(String stuff) and add the methods getName() and getStuff() to retrive the values.

You can define these methods as public and the fields name and stuff as private in order to protect the data.You can set the values like

p.setName(scanner.nextLine());
p.setStuff(scanner.nextLine());

and print the values extracting the Person reference with p.getName() and p.getStuff() in a for cycle

Thanks.

Upvotes: 0

Rostislav Matl
Rostislav Matl

Reputation: 4543

I'd suggest tio replace

ArrayList<Person> thing=new ArrayList<Person>(); 

with

List<Person> thing=new ArrayList<Person>(50);

It's more efficient as you avoid possible reallocations by specifying list's length. It's more robust as you use interface instead of class (making it easy to switch implementation e.g.).

Also consider making Person attributes private and use getters/setters to work with them.

Upvotes: 0

OscarRyz
OscarRyz

Reputation: 199274

Looks fine to start.

I'm going just to suggest a couple of things:

1.- Import the out attribute from System:

import static java.lang.System.out;

2.- In the for loop remove the break

...
for(int i=0; i<50; i++){
    Person pers = new Person();
    out.print("name: ");
    pers.name=diskScanner.nextLine();
    out.print("stuff: ");
    pers.stuff=diskScanner.nextLine();
    thing.add(pers);
    break; //<-- Remove this to continue with the remaining 49 persons
}

3.- Finally you could print the values like this:

// Display people  
for (int i=0; i<50; i++) {
    //out.println(??);{
    //}
    // could be:
    out.println("name: "+ thing.get(i).name +" stuff:"+thing.get(i).stuff );
}
...

edit

Alternatives for the print:

// introduce a variable p 
for (int i=0; i<50; i++) {
    Person p = thing.get(i);
    out.println("name: "+ p.name +" stuff:"+p.stuff );
}

Or

// using the size of the array and printf.. 
for (int i=0; i<thig.size(); i++) {
    Person p = thing.get(i);
    out.printf("name: %s stuff: %s %n", p.name ,p.stuff );
}

Or even better ( my preferred )

// using the for-each syntax
for( Person p : thing ) {
      out.printf("name: %s stuff: %s %n", p.name ,p.stuff );
}

Upvotes: 0

les2
les2

Reputation: 14479

// put the following in a file named CreatePerson.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Person {
    String name;
    String stuff;
    @Override public String toString() {
        return "Name: " + name + ", Stuff: " + stuff;
    }
}

public class CreatePerson {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<Person>(50);

        Scanner scanner = new Scanner(System.in);
        for(int i = 1; i <= 50; i++) {
            Person p = new Person();
            System.out.println("Enter person #" + i);
            System.out.print("Name: ");
            p.name = scanner.nextLine();
            System.out.print("Stuff: ");
            p.stuff = scanner.nextLine();

            people.add(p);
        }

        System.out.println("The following people were added:");
        for(Person p : people) {
            System.out.println(p);
        }
    }
}

Compile the program using: javac CreatePerson.java

Run the program using: java CreatePerson

Upvotes: 0

Frederik
Frederik

Reputation: 14556

You can go through the list using the following construct:

for (Person person: thing) {
    System.out.println(person.name + " " + person.stuff);
}

Also:

  • You might want to rename your list "persons" instead of "thing", since it contains multiple persons.
  • You might want to add a toString() method to the Person class so that a person can be displayed using System.out.println(person).
  • You might want to take the break out of the first for loop so you get all elements in the file.

Upvotes: 7

polygenelubricants
polygenelubricants

Reputation: 383886

for (Person p : thing) {
   System.out.println(p);
}

This assumes that Person has a sensible @Override public String toString() for printing. Otherwise just access the members of p as needed.

See also

Upvotes: 1

Related Questions