简体   繁体   English

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

[英]mapping an arraylist to another arraylist in java

I need your help in arraylist problem.在 arraylist 问题上,我需要您的帮助。 I have 2 arraylist.我有 2 个 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"}

Ok, array b is represent the food in a.好的,数组 b 代表 a 中的食物。 lets say让我们说

1:1:2 means apple:nut:carrot  ,
2:1:2 means grape:pistachio:carrot,  
2:3:4 means grape:walnut:tomato and 
3:4:4 means banana:peanut:tomato.

Currently I have no idea at all.目前我完全不知道。 Hopefully you guys can help me about the idea how to do this.希望你们能帮助我了解如何做到这一点。

Thanks in advance提前致谢

Well, you currently have several problems which are probably confusing the situation:好吧,您目前有几个问题可能会使情况感到困惑:

  1. There is no such class ArrayList<string> , I guess you mean List<string>没有这样的 class ArrayList<string> ,我猜你的意思是List<string>
  2. Currently your lists consist of a single element, which is a comma / space delimited string.目前,您的列表由一个元素组成,它是一个逗号/空格分隔的字符串。 You probably want something more like this:你可能想要更像这样的东西:
List fruit = new List(new string[] {"apple", "grape", "banana" });
List nut = new List(new string[] {"pistachio", "chestnut", "walnut", "peanut" });
List vegetable = new List(new string[] {"broccoli", "carrot", "cabbage", "tomato" });

This gives you a list where each element is a nut, fruit or vegetable respectively.这为您提供了一个列表,其中每个元素分别是坚果、水果或蔬菜。

Also your second list should probably look more like this:此外,您的第二个列表可能看起来更像这样:

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},
    });

Ie conbinations is a list of combinations, where each combination consists of 3 integers - the index of each element in the list.即 conbinations 是一个组合列表,其中每个组合由 3 个整数组成 - 列表中每个元素的索引。 (This is possibly a tad confusing and by no means the only option - ask if this bit isn't clear). (这可能有点令人困惑,绝不是唯一的选择——询问是否不清楚)。

In face as arrays are 0-indexed in c#, in fact you probably want this instead:面对 arrays 在 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},
    });

This at least makes your data easier to work with, so the only questions remaining are:这至少使您的数据更易于使用,因此剩下的唯一问题是:

  • How do you get from what you have to the above?你如何从你所拥有的到上述的? (I'll let you have a go at that yourself). (我会让你自己有一个 go)。
  • What is it that you are trying to do?你想做什么?

Try Below code it works as expected let me know it it does not fulfill use case.试试下面的代码,它按预期工作,让我知道它不满足用例。

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;

}

After running the program you will get below output运行程序后你会得到下面的 output

apple:pistachio:carrot
grape:pistachio:carrot
grape:walnut:tomato
banana:peanut:tomato

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM