java - 将数组中的值放入 map

标签 java arrays dictionary hashmap

我有一个整数数组,我想将它们放入 map 中。数组中的每个值都必须与数组的另一个值一起放置,这样如果我有一个 0、2、4、7 的数组,则应该有每个组合(例如 [0, 2]、[0, 4]、[0 , 7] 以及 [2, 0]、[4, 0] 和 [7,0],其余值相同)。我试图通过 Map 实现它,然后我尝试显示它。但是我对此有一些问题。我应该如何解决这个问题,或者是否有更好的方法来实现这个目标?

import java.util.*;
import java.util.Map;

public class Prac1 {
    public Map<Integer, Integer> count(int[] A){
        int k = A.length;       
        Map<Integer,Integer> m = new HashMap<Integer,Integer>();

        for (int i = 0; i < A.length; i++){
            for (int j = 0; j < A.length; j++){
                m.put(i, j);
            }
        }       
        return m;
    }
    public static void main(String[] args){
        int[] A = {0, 2, 4, 7};
        Map<Integer,Integer> m = new HashMap<Integer,Integer>();

        for (int i = 0; i < A.length; i++){
            for (int j = 0; j < A.length; j++){
                m.put(A[i], A[j]);
            }
        }           

        for (int i = 0; i < m.size(); i++){
            System.out.println(m.get(i));
        }       
    }
}

最佳答案

我不熟悉在这里回答问题 - 不过这比大脑训练要好。我试图从你的问题中推断出一些含义,并决定听起来你有一个团队列表并且想要生成一个固定装置列表。这里有一些代码来完成这项工作,虽然使用的是 String 而不是 int,但你应该明白要点......

结果是所有fixtures都存储在“fixtures”列表中,并通过第39行的语句打印出来。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FixtureGen 
{
    class Fixture
    {
        public Fixture(String teamA, String teamB)
        {
            this.teamA = teamA;
            this.teamB = teamB;
        }

        String teamA;
        String teamB;

        public String toString()
        {
            return teamA +" vs. "+teamB;
        }
    }

    public static void main(String args[])
    {
        FixtureGen gen = new FixtureGen();

        String[] teams = {"Spurs","Chelsea","Arsenal","Fulham","QPR","Palace"}; 
        List<String> teamList = Arrays.asList(teams);
        List<Fixture> fixtures = new ArrayList<Fixture>();

        for (String team : teamList)
        {
            fixtures.addAll(gen.getHomeFixtures(team, teamList));
        }

        for (Fixture fixture : fixtures)
        {
            System.out.println(fixture.toString());
        }
    }

    private  List<Fixture> getHomeFixtures(String team, List<String> teamList)
    {
        List<Fixture> fixtures = new ArrayList<Fixture>();
        Fixture fixture = null;
        for (String aTeam : teamList)
        {
            if (team != null && !team.equals(aTeam))
            {
                fixture = new FixtureGen.Fixture(team, aTeam);
                fixtures.add(fixture);
            }
        }
        return fixtures;
    }   
}

关于java - 将数组中的值放入 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9046875/

相关文章:

java - 如何使用 HttpUrlConnection 在 Android 中构建 REST 客户端

c++ - const char* str[] 中有多少个字符串?

python - 如何在 Python 中对列表中的单词进行编码

java - 使用 Jackson 的 @JsonTypeInfo 反序列化时如何保留类型属性?

java - 是否可以在带有进度监视器的 JFace 向导中运行批处理文件?

java - 将IDEA与maven2一起使用,如何添加非maven .jar?

javascript - Google map 未加载变量值

c++ - 为什么这在 C++ 字符数组中有效

python - 在一个字典值中查找一个项目,在另一个字典值中长度为1

javascript - 有没有办法在 TypeScript 中动态应用数组映射器?