【问题标题】:Use ArrayList to store information and to access the information使用 ArrayList 存储信息和访问信息
【发布时间】:2017-03-06 15:49:06
【问题描述】:

我必须创建一个程序,允许用户输入包括姓名、地址、城市、州、邮政编码和年龄在内的信息。数据结构 ArrayList 将用于存储信息。创建类来创建对象和类方法来访问信息。

  1. 创建一个名为 GroupArrayProject 的类,该类将包含 main 方法。
  2. 您将创建一个名为 People 的类,将 ArrayList 用于姓名、地址、城市、州、邮编和年龄变量。
  3. 创建一个名为 AddRecord 的方法
  4. AddRecord 将是为添加记录而调用的方法,其目的是调用将要使用的其他方法。
  5. 这些其他方法(例如 addName())将被调用并用于输入上述每个 ArrayList 的信息。
  6. 创建允许程序执行以下操作的菜单:

一个。显示您输入的所有记录

b.显示年龄最大或最小的

c。使用名字或姓氏查找记录。

public class people {
 String name, address, city, state, zipcode, age;

 public people() {
  name = null;
  address = null;
  city = null;
  state = null;
  zipcode = null;
  age = null;
 }
 public void setpeople(String n, String ad, String c, String s, String z, String a) {
  name = n;
  address = ad;
  city = c;
  state = s;
  zipcode = z;
  age = a;
 }
 public string AddRecord
 public String getpeople() {
  String p = "name:" + name + "\n" + "address:" + address + "\n" + "city:" + city + "\n" + "state:" + state + "\n" + "zip code:" + zipcode + "\n" + "age:" + age;
  return p;
 }
}

public class GroupArrayProject {
 public static void main(String[] args) {
  {
   ArrayList < String > list = new ArrayList < String > ();
   people two = new people();
   Scanner one = new Scanner(System.in);
   System.out.println("type in your name,address,city,state,zipcode and age ");
   String a = one.next();
   String b = one.next();
   String c = one.next();
   String d = one.next();
   String e = one.next();
   String f = one.next();
   two.setpeople(a, b, c, d, e, f);
   System.out.println(two.getpeople());

  }
 }
}

这就是我到目前为止所做的,我真的不知道下一步该做什么

【问题讨论】:

  • 也许首先为变量、类和方法使用正确的名称?只是一个想法..然后你的问题到底是什么?

标签: java arrays loops


【解决方案1】:

将你的 People 类重命名为 Person

邮政编码和年龄应该是整数而不是字符串

在 Person 类中创建另一个构造函数,其中包含用于初始化实例数据的参数

为类中的每个实例变量添加 getter 和 setter 方法。

将以下内容添加到 GroupArrayProject 类中:

私有静态列表 people = new ArrayList();

addRecord() 应该在 GroupArrayProject 类中。

public void addRecord(Scanner input){
    System.out.println("Name");
    String name = input.next();
    System.out.println("Address");

    ...

    people.add(new Person(...));
}

这就是基础。您只需要创建方法来搜索您的人员列表并找到匹配项,然后您就完成了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 2019-06-06
    相关资源
    最近更新 更多