K
K
Konstantin Malyarov2016-02-24 21:11:31
Java
Konstantin Malyarov, 2016-02-24 21:11:31

How to correctly pull a class from an ArrayList?

There are two classes:
1. Menu - from which the new Person class is created.
2. Person - has 4 parameters full name and DR.
Menu class:

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by Free on 23.02.2016.
 */
public class Menu {

    static ArrayList<Person.Person> arrayListPerson = new ArrayList<Person.Person>();

    public static void main (String[] agrs){
        menu();
    }

    public static void menu() {
        System.out.println("1. Создать");
        System.out.println("2. Изменить");
        System.out.println("3. Удалить");
        System.out.println("4. О системе");
        System.out.println("5. Выход");

        Scanner scannerString = new Scanner(System.in);
        String stringMenu = scannerString.nextLine();
        switch (stringMenu){
            case "1":
                Person.Person person = new Person.Person();
                System.out.println("Введите Фамилию: ");
                person.stringFamily = scannerString.nextLine();
                System.out.println("Введите имя: ");
                person.stringFirstName = scannerString.nextLine();
                System.out.println("Введите отчество: ");
                person.stringSecondName = scannerString.nextLine();
                arrayListPerson.add(person);
                System.out.println();
                menu();
                break;
            case "2":
                int intSize = arrayListPerson.size();
                while (intSize != 0){
                    System.out.println(arrayListPerson.get(arrayListPerson.size()-1));
                    intSize--;
                }
                System.out.println();

                menu();
                break;
            case "3":

                System.out.println();
                menu();
                break;
            case "4":

                System.out.println();
                break;
            case "5":
                exit();
                break;
            default:
                System.out.println("Не правильно введен пункт меню");
                System.out.println();
                menu();
                break;
        }
    }

    public static void exit() {
        System.out.println("Программа завершена!");
    }
}

Person class:
public class Person {
    public String stringFamily = null;
    public String stringFirstName = null;
    public String stringSecondName = null;
}

When adding a new instance of the Person class to the ArrayList, everything goes smoothly, but when I ask to return a value through the .get () method, then some crap comes out.
Console output:
[email protected]
[email protected]
Desired result:
Ivanov Ivan Ivanovich
Petrov Petr Petrovich
How to implement the correct data output to the console.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Elysey, 2016-02-24
@Konstantin18ko

System.out.println(arrayListPerson.get(arrayListPerson.size()-1).stringFamily + " " + arrayListPerson.get(arrayListPerson.size()-1).stringFirstName + " " + arrayListPerson.get(arrayListPerson.size( )-1).stringSecondName)

D
Denis Zagaevsky, 2016-02-24
@zagayevskiy

This is the result of calling the toString() method, which is inherited from Object. What you see is the default implementation, Full-qualified-name + @ + the address of the object in memory. You need to override this method on Person

@Override
String toString() {
    return stringFamily + stringFirstName + stringSecondName;
}

But it's not customary to use toString() for "business logic" or display to the user. It is better to make the format separately, it is believed that toString() can be changed at any time and this should not affect the application.
A couple more notes on the code:
* In Java it is not customary to write a type in a variable name.
* Class members do not need to be initialized with nulls.
* Read about 4p (public, protected, private, package-private).
* Do not make public data fields, make getters and setters. Better yet, make immutable structures.
* Read about constructors.
* Someday you will want to put your Persons in a HashMap/HashSet. Read about the equals() and hashCode() methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question