在java中将一个arraylist映射到另一个arraylist

在java中将一个arraylist映射到另一个arraylist,java,collections,arraylist,Java,Collections,Arraylist,在arraylist问题上我需要你的帮助。我有两个arraylist ArrayList<string> a = {"fruit=apple,grape,banana;nut=pistachio,chestnut,walnut,peanut;vegetable=broccoli,carrot,cabbage,tomato"} Arraylist<String> b = {"1:1:2 2:1:2 2:3:4 3:4:4"} 目前我完全不知道。希望你们能帮我解决这个问题

在arraylist问题上我需要你的帮助。我有两个arraylist

ArrayList<string> a = {"fruit=apple,grape,banana;nut=pistachio,chestnut,walnut,peanut;vegetable=broccoli,carrot,cabbage,tomato"}
Arraylist<String> b = {"1:1:2 2:1:2 2:3:4 3:4:4"}
目前我完全不知道。希望你们能帮我解决这个问题


提前感谢

嗯,您目前有几个问题可能会让情况变得混乱:

  • 没有这样的类
    ArrayList
    ,我猜你的意思是
    List
  • 当前列表由单个元素组成,该元素是逗号/空格分隔的字符串。你可能想要更像这样的东西:
  • 列表水果=新列表(新字符串[]{“苹果”、“葡萄”、“香蕉”}); List nut=新列表(新字符串[]{“开心果”、“栗子”、“胡桃”、“花生”}); 列表蔬菜=新列表(新字符串[]{“西兰花”、“胡萝卜”、“卷心菜”、“番茄”}); 这将提供一个列表,其中每个元素分别是坚果、水果或蔬菜

    另外,您的第二个列表可能更像这样:

    List<int[]> combinations = new List<int[]>(
        new int[][]
        {
            new int[] {1, 1, 2},
            new int[] {2, 1, 2},
            new int[] {2, 3, 4},
            new int[] {3, 4, 4},
        });
    
    列表组合=新列表(
    新int[][]
    {
    新的int[]{1,1,2},
    新的int[]{2,1,2},
    新的int[]{2,3,4},
    新的int[]{3,4,4},
    });
    
    也就是说,组合是一个组合列表,其中每个组合由3个整数组成——列表中每个元素的索引。(这可能有点让人困惑,而且绝不是唯一的选择——如果这一点不清楚,请询问)

    由于数组在c#中是0索引的,实际上您可能希望这样:

    List<int[]> combinations = new List<int[]>(
        new int[][]
        {
            new int[] {0, 0, 1},
            new int[] {1, 0, 1},
            new int[] {1, 2, 3},
            new int[] {2, 3, 3},
        });
    
    列表组合=新列表(
    新int[][]
    {
    新的int[]{0,0,1},
    新的int[]{1,0,1},
    新的int[]{1,2,3},
    新的int[]{2,3,3},
    });
    
    这至少使您的数据更易于使用,因此剩下的问题只有:

    • 你是如何从你所拥有的东西中得到上述东西的?(我让你自己试一试)
    • 你想做什么

    嗯,您目前有几个问题可能会让情况变得混乱:

  • 没有这样的类
    ArrayList
    ,我猜你的意思是
    List
  • 当前列表由单个元素组成,该元素是逗号/空格分隔的字符串。你可能想要更像这样的东西:
  • 列表水果=新列表(新字符串[]{“苹果”、“葡萄”、“香蕉”}); List nut=新列表(新字符串[]{“开心果”、“栗子”、“胡桃”、“花生”}); 列表蔬菜=新列表(新字符串[]{“西兰花”、“胡萝卜”、“卷心菜”、“番茄”}); 这将提供一个列表,其中每个元素分别是坚果、水果或蔬菜

    另外,您的第二个列表可能更像这样:

    List<int[]> combinations = new List<int[]>(
        new int[][]
        {
            new int[] {1, 1, 2},
            new int[] {2, 1, 2},
            new int[] {2, 3, 4},
            new int[] {3, 4, 4},
        });
    
    列表组合=新列表(
    新int[][]
    {
    新的int[]{1,1,2},
    新的int[]{2,1,2},
    新的int[]{2,3,4},
    新的int[]{3,4,4},
    });
    
    也就是说,组合是一个组合列表,其中每个组合由3个整数组成——列表中每个元素的索引。(这可能有点让人困惑,而且绝不是唯一的选择——如果这一点不清楚,请询问)

    由于数组在c#中是0索引的,实际上您可能希望这样:

    List<int[]> combinations = new List<int[]>(
        new int[][]
        {
            new int[] {0, 0, 1},
            new int[] {1, 0, 1},
            new int[] {1, 2, 3},
            new int[] {2, 3, 3},
        });
    
    列表组合=新列表(
    新int[][]
    {
    新的int[]{0,0,1},
    新的int[]{1,0,1},
    新的int[]{1,2,3},
    新的int[]{2,3,3},
    });
    
    这至少使您的数据更易于使用,因此剩下的问题只有:

    • 你是如何从你所拥有的东西中得到上述东西的?(我让你自己试一试)
    • 你想做什么

    尝试下面的代码,它按预期工作,让我知道它不符合用例

    public static List<String> fruits = new ArrayList<String>();
    public static List<String> nuts = new ArrayList<String>();
    public static List<String> vegitables = new ArrayList<String>();
    
    /**
     * @param args
     * @throws ParseException
     * @author Rais.Alam
     */
    public static void main(String[] args) throws ParseException
    {
    
        fruits.add("apple");
        fruits.add("grape");
        fruits.add("banana");
    
        nuts.add("pistachio");
        nuts.add("chestnut");
        nuts.add("walnut");
        nuts.add("peanut");
    
        vegitables.add("broccoli");
        vegitables.add("carrot");
        vegitables.add("cabbage");
        vegitables.add("tomato");
    
        System.out.println(getValue("1:1:2"));
        System.out.println(getValue("2:1:2"));
        System.out.println(getValue("2:3:4"));
        System.out.println(getValue("3:4:4"));
    
    }
    
    public static String getValue(String key)
    {
        String returnString = "";
        String[] arr = key.split(":");
        returnString += fruits.get(Integer.parseInt(arr[0]) - 1) == null ? "" : fruits.get(Integer.parseInt(arr[0]) - 1) + ":";
        returnString += nuts.get(Integer.parseInt(arr[1]) - 1) == null ? "" : nuts.get(Integer.parseInt(arr[1]) - 1) + ":";
        returnString += vegitables.get(Integer.parseInt(arr[2]) - 1) == null ? "" : vegitables.get(Integer.parseInt(arr[2]) - 1);
    
        return returnString;
    
    }
    

    试试下面的代码,它可以正常工作,让我知道它不符合用例

    public static List<String> fruits = new ArrayList<String>();
    public static List<String> nuts = new ArrayList<String>();
    public static List<String> vegitables = new ArrayList<String>();
    
    /**
     * @param args
     * @throws ParseException
     * @author Rais.Alam
     */
    public static void main(String[] args) throws ParseException
    {
    
        fruits.add("apple");
        fruits.add("grape");
        fruits.add("banana");
    
        nuts.add("pistachio");
        nuts.add("chestnut");
        nuts.add("walnut");
        nuts.add("peanut");
    
        vegitables.add("broccoli");
        vegitables.add("carrot");
        vegitables.add("cabbage");
        vegitables.add("tomato");
    
        System.out.println(getValue("1:1:2"));
        System.out.println(getValue("2:1:2"));
        System.out.println(getValue("2:3:4"));
        System.out.println(getValue("3:4:4"));
    
    }
    
    public static String getValue(String key)
    {
        String returnString = "";
        String[] arr = key.split(":");
        returnString += fruits.get(Integer.parseInt(arr[0]) - 1) == null ? "" : fruits.get(Integer.parseInt(arr[0]) - 1) + ":";
        returnString += nuts.get(Integer.parseInt(arr[1]) - 1) == null ? "" : nuts.get(Integer.parseInt(arr[1]) - 1) + ":";
        returnString += vegitables.get(Integer.parseInt(arr[2]) - 1) == null ? "" : vegitables.get(Integer.parseInt(arr[2]) - 1);
    
        return returnString;
    
    }
    

    因为b列表来自ArrayList。实际上它是一个很长的列表,但我只是将其缩短以制作一个简单的示例。但是对于a,我认为我可以将其更改为list或double array。@Roubie在我看来,它就像是在解析一些字符串(例如,来自文本文件)在这种情况下,你构建列表的方式会有所不同——无论如何,我希望这个答案能给你一些指导。因为b列表来自ArrayList。实际上,它是一个长列表,但我只是将其缩短以制作一个简单的示例。但对于a,我想我可以将其更改为list或double array。@Roubie在我看来,你好像在解析一些字符串(例如,来自文本文件),在这种情况下,您构建列表的方式会有所不同-无论如何,我希望这个答案能帮助您获得一些指导。继续验收。继续验收。