尝试链接2个单独的数组列表中的对象

时间:2018-04-30 18:30:46

标签: java

感谢您抽出宝贵时间来看看这个。我的一个项目遇到了一些麻烦。截至目前,我有2个收集名称和ID的数组列表。我想将两个列表的对象(名称和id)相互链接,以便稍后我可以执行插入排序算法。

任务:
1)要求用户输入团队成员的姓名和ID。用户应在单独的行上交替输入名称和ID,如下面的示例运行所示。提供信息后,TeamMembers将添加到TeamMember对象的ArrayList中。请记住,名称应存储在TeamMember类中的标题大小写中。

2)用户应输入单词" STOP"以小写和大写字母的任意组合,以停止输入团队成员信息。

3)使用插入排序算法按ID递增排序ArrayList。在将每个新的TeamMember对象插入到数组中时,可以选择使用插入排序算法,也可以等到整个数组构建完成后再排序所有成员。

4)输入并排序所有名称后,使用ArrayList.toString()打印ArrayList的内容。

package com.company;

import java.util.*;
import java.util.HashMap;

public class Main {

public static void main(String[] args) {
    String inputName;
    String inputID;

    List<String> fullName = new ArrayList<String>();
    List<String> idString = new ArrayList<String>();

    while (!(fullName.contains("stop"))) {
        Scanner uInput = new Scanner(System.in);
        //String checkInput = "test";
        System.out.println("Please enter the name: ");
        inputName = uInput.nextLine();

        fullName.add(inputName.toLowerCase());

        // System.out.println("List: " + fullName);  //Check what is in the fullName List
        // System.out.println("List Size: " + fullName.size()); // Check the size of fullName ArrayList


        System.out.println("Please enter the ID: ");
        inputID = uInput.nextLine();

        idString.add(inputID.toLowerCase());
    }
    fullName.remove(fullName.size() - 1);  //removing the last word, which is always "stop"
    idString.remove(idString.size() - 1);    //(Same as above)

    //insertionSort(fullName);
    //insertionSort(idString);
}

HashMap newmap = new HashMap(); //creating hash map

Best,Nate

1 个答案:

答案 0 :(得分:1)

  

提供信息后,TeamMembers将被添加到TeamMember对象的ArrayList

因此,您需要一个具有名称和ID的TeamMember

class TeamMember {
    private String name;
    private String id;

    public TeamMember(String name, String id) {
        this.name = name;
        this.id = id;
    }
}
  

请记住,名称应存储在TeamMember类的标题内。

有关将字符串转换为标题大小写

,请参阅this post

您余下工作的线索:

  

用户应以小写和大写字母的任意组合输入单词“STOP”,以停止输入团队成员信息。

获取名称后,使用String的equalsIgnoreCase检查输入的String是否为STOP的变体。

  

使用插入排序算法按ID递增排序ArrayList。

您必须传递Comparator(到排序方法)或使TeamMember实施Comparable来比较两个TeamMember个对象

相关问题