【问题标题】:How to let java check whether people are friends or not? [closed]如何让java检查人们是否是朋友? [关闭]
【发布时间】:2015-04-21 12:07:09
【问题描述】:

我如何检查人们是否是朋友。

输入看起来像

Hans Peter
Thomas Peter
Hans Thomas
Kate Thomas

然后我会检查 Hans Thomas 和 Peter 是否是预期输出为“是”的朋友

我的想法是创建一个读取行的缓冲读取器和一个字符串标记器,该器从缓冲读取器获取输入并将它们放入一个数组列表中。

【问题讨论】:

  • 好的,你试过了吗?你在哪里卡住了?
  • 执行你的计划然后来这里
  • 不知道是不是朋友
  • 您可以使用 Map>,其中每个人都按其名称映射到其朋友列表。
  • @user4118143 试一试,首先读取数据并以某种方式存储它,如果您遇到困难,请返回您自己编写的代码。

标签: java arraylist bufferedreader


【解决方案1】:

您可以尝试一种更面向对象的方法,方法是将您的人包装在一个 Person 对象中,为每个对象维护一个朋友列表。提供API来添加,检索和检查两个人之间的友谊。大概是这样的:

public interface FriendShip <T>{
    public boolean isFriend(T t);
    public List<T> getFriends();
    public void addFriend(T t);
}


public class Person implements FriendShip<Person> {
    List<Person> friendList = null;

    @Override
    public boolean isFriend(Person t) {
        return friendList.contains(t);
    }

    @Override
    public List<Person> getFriends() {
        return friendList;
    }

    @Override
    public void addFriend(Person newFriend) {
        friendList.add(newFriend);
        if(!newFriend.isFriend(this)){
            newFriend.addFriend(this);
        }
    }
}

【讨论】:

    【解决方案2】:

    一些不完美的解决方案是建立朋友地图:

    Map<String, List<String>> friends = new HashMap();
    
    List<String> hansFriends = new ArrayList();
    hansFriends.add("Peter");
    hansFriends.add("Thomas");
    //and so on for each person
    //...
    
    //then add friends for "Hans"
    peopleFriends.put("Hans", hansFriends);
    
    //repeat for each person: Kate, Thomas
    katesFriends = new ArrayList();
    katesFriends.add("Thomas");
    //...
    thomasFriends = new ArrayList();
    

    这将转化为以下示例:

    import java.util.Map;
    import java.util.HashMap;
    import java.util.List;
    import java.util.ArrayList;
    import java.io.Console;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    
    public class WhoFriends
    {
        /**
         * This is for reading friends. Peter,Hans or Peter; Hans or Peter, Hans works.
         */
        public static final String FRIENDS_PATTERN = "(\\w+)\\W+(\\w+)"; 
    
        /**
         * We store friends to a map person -> {friends list}
         */
        Map<String, List<String>> friends = new HashMap<String, List<String>>();
    
        /**
         * Getter for friends list.
         */
        public Map<String, List<String>> getFriends()
        {
            return friends;
        }
    
        /**
         * Factory method for friends list.
         */
        public List<String> newFriendsList()
        {
            return new ArrayList<String>();
        }
    
        /**
         * Show friends information.
         */
        public void printInfo(String person1, String person2)
        {
            System.out.format("Are %s and %s friends? Answer: %b.%n", person1, person2, areFriends(person1, person2)); 
        }
    
        /**
         * Show all we have information
         */
        public void showFriends()
        {
            for(String person : getFriends().keySet())
            {
                System.out.format("%s is friends with: %s%n", person, getFriendsList(person).toString());
            }
        }
    
        /**
         * This also adds an empty friends list if no list found for the person.
         */
        public List<String> getFriendsList(String person)
        {
            // have an empty list if no friends.
            List<String> friendsList = newFriendsList();
    
            if (getFriends().containsKey(person))
            {
                friendsList = getFriends().get(person);
            }
            else
            {
                // add empty friends list
                getFriends().put(person, friendsList);
            }
    
            return friendsList;
        }
    
        /**
         * Check if a person is friend with another person.
         */
        public boolean isFriendOf(String person, String possiblyAFriend)
        {
            return getFriendsList(person).contains(possiblyAFriend);
        }
    
        public void makeFriends(String person1, String person2)
        {
            addFriend(person1, person2);
            addFriend(person2, person1);
        }
    
        public void addFriend(String person, String friend)
        { 
            getFriendsList(person).add(friend);
        }
    
        /**
         * Two people are friends if they are to each other friends.
         */
        public boolean areFriends(String person1, String person2)
        {
            return isFriendOf(person1, person2) && isFriendOf(person2, person1);
        }
    
        public void readFriends(String line)
        {
            Pattern friendsExpression = Pattern.compile(FRIENDS_PATTERN);
            Matcher matcher           = friendsExpression.matcher(line);
            //Read pairs: Person1, Person2
            if (matcher.find() && (2 == matcher.groupCount()))
            {
                String person1 = matcher.group(1);
                String person2 = matcher.group(2);
                System.out.format("Making friends from %s: %s and %s are friends now.%n",
    
                        matcher.group(0), 
                        person1,
                        person2);
                makeFriends(person1, person2);
            }
        }
    
        /**
         * Demonstration :)
         */
        public static void main(String[] args)
        {
            WhoFriends wf = new WhoFriends();
    
            /**
             * Predefine some friendships.
             */
            wf.makeFriends("Hans"  , "Peter");
            wf.makeFriends("Thomas", "Peter");
            wf.makeFriends("Hans"  , "Thomas");
            wf.makeFriends("Kate"  , "Thomas");
    
            wf.printInfo("Hans"  , "Peter");
            wf.printInfo("Thomas", "Peter");
            wf.printInfo("Hans"  , "Thomas");
            wf.printInfo("Kate"  , "Thomas");
            wf.printInfo("Kate"  , "Peter");
            wf.printInfo("Peter" , "Thomas");
    
            /**
             * Make friends
             */
            Console console = System.console();
            String  line = null;
            System.out.println("Reading friends. Type in each line Name1,Name2. When you don't want to make more friends type q on the line.");
            /**
             */
            while (!"q".equals(line = console.readLine()))
            { 
                wf.readFriends(line);
            }
            wf.showFriends();
        }
    }
    

    【讨论】:

    • 那你如何检查其中一些是否是朋友?
    • @user4118143 您查看地图以查看某人的朋友列表。上面示例中的方法 areFriends(String, String)。 :)
    【解决方案3】:

    我的想法是创建一个读取行的缓冲区读取器和一个从缓冲区读取器获取输入并放入的字符串标记器...

    到目前为止非常好!

    ...它们在一个 Arraylist 中。

    瞄准你的目标,你可以想出一个更好的结构......(但在这一点上,我已经可以欢迎你使用图论了!):-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多