簡體   English   中英

將 arraylist 映射到 java 中的另一個 arraylist

[英]mapping an arraylist to another arraylist in java

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

好的,數組 b 代表 a 中的食物。 讓我們說

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.

目前我完全不知道。 希望你們能幫助我了解如何做到這一點。

提前致謝

好吧,您目前有幾個問題可能會使情況感到困惑:

  1. 沒有這樣的 class 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},
    });

即 conbinations 是一個組合列表,其中每個組合由 3 個整數組成 - 列表中每個元素的索引。 (這可能有點令人困惑,絕不是唯一的選擇——詢問是否不清楚)。

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

這至少使您的數據更易於使用,因此剩下的唯一問題是:

  • 你如何從你所擁有的到上述的? (我會讓你自己有一個 go)。
  • 你想做什么?

試試下面的代碼,它按預期工作,讓我知道它不滿足用例。

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;

}

運行程序后你會得到下面的 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