开发者

mapping an arraylist to another arraylist in java

I need your help in arraylist problem. I have 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. 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.

Current开发者_如何学Goly 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>
  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},
    });

I.e. conbinations is a list of combinations, where each combination consists of 3 integers - the index of each element in the list. (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:

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).
  • 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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜