访问ArrayList中的ArrayList

时间:2018-09-06 23:29:32

标签: java arrays arraylist

我已将ArrayLists(成员)作为一个ArrayList(成员)。因此,“成员”具有朋友列表。

此方法接收成员的名称,其目的是列出所有成员的朋友。我很困惑如何从成员列表中访问朋友的ArrayList。

public java.lang.String[] getPals(java.lang.String name) {

    if(members.contains(name)) {
        memberFriends.get(members.indexOf(name));
        String[] friends = memberFriends.toArray(new String[memberFriends.size()]);
    }
    return friends;

}

所以基本上,我想在这里做的是,说朋友的名字叫Joe,我想去ArrayList成员中的Joe,并将他朋友的ArrayList变成一个数组。我只是对如何联系乔的朋友感到困惑。我在上面的第三行代码中尝试执行的操作是访问名称“ Joe”。现在我很困惑如何访问Joey的ArrayList的朋友。

2 个答案:

答案 0 :(得分:0)

这看起来像泛型的情况。如果您希望访问memberFriends.get(0)(将打印一个数组),则需要像这样指示memberFriends:

List<List<String>> memberFriends = new ArrayList<List<String>>();

不知道我是否理解:您可以尝试迭代:

for (List<String> member : memberFriends) {

  for (String s : member) {

   
  }
}

答案 1 :(得分:0)

假设您可以控制代表成员及其朋友的方式,那么我建议这样做:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Example {

    public static class Member {
        String name;

        public Member(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Member [name=" + name + "]";
        }
    }

    public static void main(String[] args) {
        Map<Member, ArrayList<Member>> members = new HashMap<>();
        populateMembers(members);
        System.out.println(members);
        getPals(members, "Ross");
    }

    private static Member[] getPals(Map<Member, ArrayList<Member>> members, String name) {
        if (members.get(name) != null) {
            return members.get(name).toArray(new Member[members.get(name).size()]);
        } else {
            return new Member[] {};
        }
    }

    private static void populateMembers(Map<Member, ArrayList<Member>> members) {
        String[] names = new String[] { "Joe", "David", "Ross", "Monica", "James" };
        for (int i = 0; i < names.length; i++) {
            ArrayList<Member> friends = new ArrayList<>();
            for (int j = i+1; j < names.length; j++) {
                friends.add(new Member(names[j]));
            }
            members.put(new Member(names[i]), friends);
        }
    }
}

第二种方法(如果您坚持使用两个数组列表,members和memberFriends:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Example {

    public static class Member {
        String name;

        public Member(String name) {
            this.name = name;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Member other = (Member) obj;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }

        @Override
        public String toString() {
            return "Member [name=" + name + "]";
        }
    }

    public static ArrayList<Member> members = new ArrayList<>();
    public static ArrayList<ArrayList<Member>> memberFriends = new ArrayList<>();

    public static void main(String[] args) {
        populateMembers();
        System.out.println(members);
        System.out.println(memberFriends);
        System.out.println(Arrays.toString(getPals("Ross")));
    }

    private static Member[] getPals(String name) {
        int index = members.lastIndexOf(new Member(name));
        if (index != -1) {
            return memberFriends.get(index).toArray(new Member[memberFriends.get(index).size()]);
        } else {
            return new Member[] {};
        }
    }

    private static void populateMembers() {
        String[] names = new String[] { "Joe", "David", "Ross", "Monica", "James" };
        for (int i = 0; i < names.length; i++) {
            ArrayList<Member> friends = new ArrayList<>();
            for (int j = i + 1; j < names.length; j++) {
                friends.add(new Member(names[j]));
            }
            members.add(new Member(names[i]));
            memberFriends.add(friends);
        }
    }
}