从二维数组中的所有值填充一维数组

时间:2019-03-01 09:50:45

标签: java arrays

假设我有一个带有int值的二维数组,并且我想用该二维数组中的所有int值填充一维数组,我该怎么做?

这是我尝试过的方法,但是出了点问题,我不知道该怎么办。

int[][] twoDimensionalArray = {{5, 2, 3, 1},
                               {4, 2, 6, 9},
                               {8, 9, 1, 8}};
int[] oneDimensionalArray = new int[twoDimensionalArray.length * twoDimensionalArray.length];
for (int i = 0; i < twoDimensionalArray.length; i++) {
    for (int j = 0; j < twoDimensionalArray.length; j++) {
      oneDimensionalArray[i] = twoDimensionalArray[i][j];
     }
}

谢谢!

4 个答案:

答案 0 :(得分:1)

我可以建议类似的东西

String[][] my2Darr = {{5, 2, 3, 1},
                      {4, 2, 6, 9},
                      {8, 9, 1, 8}};
    List<String> list = new ArrayList<>();
    for(int i = 0; i < my2Darr.length; i++) {
        list.addAll(Arrays.asList(my2Darr[i])); // java.util.Arrays
    }
    String[] my1Darr = new String[list.size()];
    my1Darr = list.toArray(my1Darr);

Java 8:

int[][] 2darr = {{5, 2, 3, 1},
               {4, 2, 6, 9},
               {8, 9, 1, 8}};
int[] 1darr = Stream.of(2darr ).flatMapToInt(IntStream::of).toArray();
System.out.println(Arrays.toString(1darr ));

答案 1 :(得分:1)

SQL server we have to create table like this format only there is no other styles 

 DECLARE @StructuredProducts TABLE(
StructuredProductId INT
,ArrangerIdv2 INT
,ISIN VARCHAR(50)
,InstrumentId INT
,Caption VARCHAR(100)
,DbRegDate DATE
,EndDate DATE
) ;
DECLARE @PriceDataOld TABLE(
StructuredProductId INT
,FinalDate DATE
,FinalPriceDate DATE
,FinalPrice DECIMAL(9,3)
,FinalPriceSek DECIMAL(9,3)
);
DECLARE @PriceDataEarlyExercise TABLE(
StructuredProductId INT
,EEDate DATE
,EEPriceDate DATE
,EEPrice DECIMAL(9,3)
,EEPriceSek DECIMAL(9,3)
);
DECLARE @PriceDataActive TABLE(
StructuredProductId INT
,ActiveDate DATE
,ActivePriceDate DATE
,ActivePrice DECIMAL(9,3)
,ActivePriceSek DECIMAL(9,3)
);    

您的解决方案几乎是正确的。您必须将int[][] twoDimensionalArray = {{5, 2, 3, 1}, {4, 2, 6, 9}, {8, 9, 1, 8}}; int[] oneDimensionalArray = new int[twoDimensionalArray.length * twoDimensionalArray.length]; for (int i = 0; i < twoDimensionalArray.length; i++) { for (int j = 0; j < twoDimensionalArray[0].length; j++) { oneDimensionalArray[i] = twoDimensionalArray[i][j]; } } 改成twoDimensionalArray.length。因为您要水平和垂直地迭代。如果对twoDimensionalArray.length进行2倍的迭代,则将对水平轴的长度进行2倍的迭代。

答案 2 :(得分:0)

您可以使用stream进行操作,例如:

int[][] twoDimensionalArray = {{5, 2, 3, 1},
        {4, 2, 6, 9},
        {8, 9, 1, 8}};

int[] oneDimensionalArray = Arrays.stream(twoDimensionalArray)
    .flatMapToInt(e -> Arrays.stream(e))
    .toArray();

System.out.println(Arrays.toString(oneDimensionalArray));

答案 3 :(得分:0)

您可以使用它,它使用System.arraycopy()将内部数组复制到结果中:

public static int[] flatten(int[][] input) {
    int length = input.length;
    if(length == 0) return new int[0];

    int[] output = new int[length * input[0].length];
    for(int i = 0; i < length; i++) {
       int[] inner = input[i];
       // 1. Parameter: the source to copy from
       // 2. Parameter: the starting index from source
       // 3. Parameter: the destionation to copy to
       // 4. Parameter: the starting index from destination
       // 5. Parameter: the amount of elements to copy
       System.arraycopy(inner, 0, output, i * length, inner.length);
    }
    return output;
}
相关问题