Answer the question
In order to leave comments, you need to log in
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("Программа завершена!");
}
}
public class Person {
public String stringFamily = null;
public String stringFirstName = null;
public String stringSecondName = null;
}
Answer the question
In order to leave comments, you need to log in
System.out.println(arrayListPerson.get(arrayListPerson.size()-1).stringFamily + " " + arrayListPerson.get(arrayListPerson.size()-1).stringFirstName + " " + arrayListPerson.get(arrayListPerson.size( )-1).stringSecondName)
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;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question