2011-04-28 121 views
0

我需要您在列表列表问题中的帮助。我有2个阵列表。将阵列列表映射到java中的另一个阵列列表

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"} 

好的,数组b是表示食品中。可以说

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. 

目前我完全不知道。希望你们可以帮助我了解如何做到这一点。

在此先感谢

+0

工作。 – 2013-01-09 13:36:38

回答

2

好了,你现在有哪些是可能混淆的情况几个问题:

  1. 没有这样的类ArrayList<string>,我猜你的意思是List<string>
  2. 目前您的列表由单个元素,这是一个逗号/空格分隔的字符串。你可能想要更多的东西是这样的:
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" }); 

这给你一个列表,其中每个元素分别是坚果,水果或蔬菜。

而且你的第二个列表应该看起来可能是这样的:

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

即结合是一个组合列表,其中每个组合由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}, 
    }); 

这至少让你的数据更易于使用,所以剩下的唯一问题是:

  • 你是如何从上面得到的? (我会让你自己去看看)。
  • 你想要做什么?
+0

为b列表是从ArrayList 。实际上它是一个很长的列表,但我只是简短地做了一个简单的例子。但对于我认为我可以将其更改为列表或双数组。 – Roubie 2011-04-28 02:31:24

+0

@Roubie在我看来,你是在解析一些字符串(例如来自文本文件),在这种情况下,你构建列表的方式会有所不同 - 无论如何,我希望这个答案能帮助你给出一些方向。 – Justin 2011-04-28 02:46:36

1

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

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; 

} 

运行程序后,你会得到下面的输出上您接受

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