如何在另一个列表中添加元素到列表?

时间:2016-10-18 13:04:46

标签: java list

我有一个名为Utilizador的类,它使用以下属性创建:

CREATE PROCEDURE [dbo].[sp_CustomerDataList] 
AS
BEGIN
    SET NOCOUNT ON;
SELECT cu.FirstName, cu.LastName, cu.Phone1, co.CountryName, n.Nationality 
FROM Customer cu
Left Join Country co
ON cu.CountryId = co.CountryId
Left Join Nationality n
ON cu.NationalityId = n.NationalityId
WHERE cu.IsDeleted = 0
END

在我的主要课程中,我有一个带有followinf格式的Utilizadores列表:

private String nome;
private String email;
private String cidadeAtual;
private List<String> checkins = new ArrayList<String>();
private List<String> amigos = new ArrayList<String>();

我要做的是,键入一个名称,然后输入另一个名称并添加友谊。例如:

我输入Daniel,按回车键。我输入Maria,按回车键。列表中的amigos现在必须具有这样的名字:

Nickname: Daniel | Email: dd | Cidade atual: Maia | Checkins: [Maia] | Amigos: []
Nickname: Maria | Email: dd2 | Cidade atual: Porto | Checkins: [Porto] | Amigos: []

但我开发的方法将这两个名字放在Amigos列表中,如下所示:

Nickname: Daniel | Email: dd | Cidade atual: Maia | Checkins: [Maia] | Amigos: [Maria]
Nickname: Maria | Email: dd2 | Cidade atual: Porto | Checkins: [Porto] | Amigos: [Daniel]

以下是方法:

Nickname: Daniel | Email: dd | Cidade atual: Maia | Checkins: [Maia] | Amigos: [Maria, Daniel]
Nickname: Maria| Email: dd2 | Cidade atual: Porto | Checkins: [Porto] | Amigos: [Maria, Daniel]

2 个答案:

答案 0 :(得分:0)

这样的事情就是一个开始。

HashMap<String, String> Amigos = new HashMap<>();

//Create Friendship between Marian and Daniel.
Amigos.put("Maria", "Daniel");

//Get Maria's Friend.
System.out.println(Amigos.get("Maria"))

答案 1 :(得分:0)

您也可以采用对象方式。

人物对象

import java.util.ArrayList;


public class Person {
    String name;
    ArrayList<String> friends = new ArrayList();

    Person(String name)
    {
        this.name = name;
    }

    String getName()
    {
        return this.name;
    }

    void addFriend(String friendName)
    {
        friends.add(friendName);
    }

    boolean isFriend(String friendName)
    {
        for(String friend : this.friends)
        {
            if(friend.equals(friendName))
            {
                return true;
            }
        }
        return false;
    }

    String getFriendInPosition(int position)
    {
        return this.friends.get(position);
    }

    int getFriendPosition(String friendName)
    {
        int i = 0;
        for(String friend : this.friends)
        {
            if(friend.equals(friendName))
            {
                return i;
            }
            i++;
        }
        return -1;
    }

    ArrayList<String> getAllFriends()
    {
        return this.friends;
    }
}

主类

public static void main(String[] args) {       

  Person maria = new Person("Maria");
  Person daniel = new Person("Daniel");

  maria.addFriend(daniel.getName());
  daniel.addFriend("Maria");//You could create this to add Person objects as friends

  maria.addFriend("me");//You could create this to add Person objects as friends
  maria.addFriend("you");//You could create this to add Person objects as friends

  System.out.println(maria.getFriendInPosition(0));

  System.out.print("Are Maria and Daniel friends? ");
  System.out.println(maria.isFriend(daniel.getName()));

  for(String friend : maria.getAllFriends())
  {
      System.out.println("Maria's friend: " + friend);
  }

  for(String friend : daniel.getAllFriends())
  {
      System.out.println("Daniel's friend: " + friend);
  }
}