从2D数组值写入字符串数组

时间:2017-10-18 08:34:23

标签: java arrays sorting

您好我不知道如何将Array [] []中的特定值写入String []数组。例如:

String[][] matrix = new String[offerArray.length][3];

for (int i = 0; i < offerArray.length; i++) {
    String[] tempArray = offerArray[i].split("; ");

    for (int j = 0; j < tempArray.length; j++) {
        matrix[i][j] = tempArray[j];
    }
}

// Result
String [0][1] = Name;
String [0][2] = Price;
String [0][3] = imgURL;

String [1][1] = Name;
String [1][2] = Price;
String [1][3] = imgURL;

String [2][1] = Name;
String [2][2] = Price;
String [2][3] = imgURL;


//Now I want to write the values into seperat Strings[] but how?
String [] all_name;
String [] all_price;
String [] all_imgURL;

我怎么能这样做?请帮帮我。

我搜索的解决方案:

 String [] all_name = new String[offerArray.length];
 String [] all_price = new String[offerArray.length];
 String [] all_imgURL = new String[angebotArr.length];

 for (int i = 0; i < offerArray.length; i++){
     all_name [i] = matrix[i][0];
     all_price [i] = matrix[i][1];
     all_imgURL [i] = matrix[i][2];        
  }

2 个答案:

答案 0 :(得分:1)

数据格式

因此,如果我纠正您的2D数据,请执行以下操作:

// First item
data[0][0] -> Name   of 1. item
data[0][1] -> Price  of 1. item
data[0][2] -> ImgURL of 1. item

// Second item
data[1][0] -> Name   of 2. item
data[1][1] -> Price  of 2. item
data[1][2] -> ImgURL of 2. item

你想要达到这样的结果:

// All name values
data[0] -> Name of 1. item
data[1] -> Name of 2. item
...
// All price values
data[x + 0] -> Price of 1. item
data[x + 1] -> Price of 2. item
...
// All imgURL values
data[x + y + 0] -> ImgURL of 1. item
data[x + y + 1] -> ImgURL of 2. item

转换

您可以先使用Map轻松转换,然后收集所有数据,然后将按键转换为数组。这是它的样子:

String[][] data = ...
Map<String, ArrayList<String>> typeToData = new HashMap<>();

// Collect all data
for (int item = 0; item < data.length; item++) {
    // Get data for that item
    String name = data[item][0];
    String price = data[item][1];
    String imgUrl = data[item][2];

    // Add name data
    String[] nameData = typeToData.getOrDefault("Name", new ArrayList<>())
    nameData.add(name);
    typeToData.put("Name", nameData);

    // Add price data
    String[] priceData = typeToData.getOrDefault("Price", new ArrayList<>())
    priceData.add(price);
    typeToData.put("Price", priceData);

    // Add ImgURL data
    String[] imgUrlData = typeToData.getOrDefault("ImgURL", new ArrayList<>())
    imgUrlData.add(imgUrl);
    typeToData.put("Name", imgUrlData);
}

// Transform the data into the final array format
String[] result = new String[data.length * data[0].length];

// Set name data
int item = 0
for (String name : typeToData.get("Name")) {
    result[item] = name;
    item++;
}

// Set price data
for (String price : typeToData.get("Price")) {
    result[item] = price;
    item++;
}

// Set imgUrl data
for (String imgUrl : typeToData.get("ImgURL")) {
    result[item] = imgUrl;
    item++;
}

字段result现在具有所需的结构。

更通用

您当然可以删除类型标签(名称,价格,imgUrl)来创建更紧凑和通用的解决方案,如下所示:

String[][] data = ...
ArrayList<ArrayList<String>> typeToData = new ArrayList<>();

// Collect all data
for (int item = 0; item < data.length; item++) {
    // Traverse all data of that item
    for (int type = 0; type < data[item].length; type++) {
        ArrayList<String> dataOfType = typeToData.get(type);

        // Create list if absent
        if (dataOfType == null) {
            dataOfType = new ArrayList<>();
        }

        // Add value and set
        dataOfType.add(data[item][type];
        typeToData.set(type, dataOfType);
    }
}

// Transform the data into the final array format
String[] result = new String[data.length * data[0].length];

// Set data
int entry = 0;
// Iterate type-wise
for (int type = 0; type < typeOfData.size(); type++) {
    // Iterate all items
    ArrayList<String> dataOfType = typeOfData.get(type);
    for (int item = 0; item < dataOfType.size(); item++) {
        result[entry] = dataOfType.get(item);
        entry++;
    }
}

或者在最后一部分使用Stream#flatMap来实现更紧凑的版本:

// Transform the data into the final array format
String[] result = typeToData.stream()  // Stream of ArrayList<String>
    .flatMap(ArrayList::stream)        // Stream of String
    .toArray(String[]::new);           // Convert to String[]

单独的数据阵列

如果要将所有类型的数据分开,可以使用中间结果typeToData。它恰好包含了这一点。

如果您使用typeToDataMap的版本:

int amoutOfItems = data.length;

String[] allNames = typeToData.get("Name")
    .toArray(new String[amoutOfItems));
String[] allPrices = typeToData.get("Price")
    .toArray(new String[amoutOfItems));
String[] allImgUrl = typeToData.get("ImgURL")
    .toArray(new String[amoutOfItems));

对于typeToDataArrayList的版本:

int amoutOfItems = data.length;

String[] allNames = typeToData.get(0)
    .toArray(new String[amoutOfItems));
String[] allPrices = typeToData.get(1)
    .toArray(new String[amoutOfItems));
String[] allImgUrl = typeToData.get(2)
    .toArray(new String[amoutOfItems));

答案 1 :(得分:0)

我搜索的解决方案:

            String [] all_name = new String[offerArray.length];
            String [] all_price = new String[offerArray.length];
            String [] all_imgURL = new String[angebotArr.length];

            for (int i = 0; i < offerArray.length; i++)
                {
                    all_name [i] = matrix[i][0];
                    all_price [i] = matrix[i][1];
                    all_imgURL [i] = matrix[i][2];        
                }