Convert a 2D array into a 1D array

2020-02-06 06:05发布

问题:

Here is the code I have so far:

 public static int mode(int[][] arr) {
      ArrayList<Integer> list = new ArrayList<Integer>();
      int temp = 0;
      for(int i = 0; i < arr.length; i ++) {
          for(int s = 0; s < arr.length; s ++) {
              temp = arr[i][s];

I seem to be stuck at this point on how to get [i][s] into a single dimensional array. When I do a print(temp) all the elements of my 2D array print out one a time in order but cannot figure out how to get them into the 1D array. I am a novice :(

How to convert a 2D array into a 1D array?

The current 2D array I am working with is a 3x3. I am trying to find the mathematical mode of all the integers in the 2D array if that background is of any importance.

回答1:

You've almost got it right. Just a tiny change:

public static int mode(int[][] arr) {
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < arr.length; i++) {
        // tiny change 1: proper dimensions
        for (int j = 0; j < arr[i].length; j++) { 
            // tiny change 2: actually store the values
            list.add(arr[i][j]); 
        }
    }

    // now you need to find a mode in the list.

    // tiny change 3, if you definitely need an array
    int[] vector = new int[list.size()];
    for (int i = 0; i < vector.length; i++) {
        vector[i] = list.get(i);
    }
}


回答2:

change to:

 for(int i = 0; i < arr.length; i ++) {
          for(int s = 0; s < arr[i].length; s ++) {
              temp = arr[i][s];


回答3:

"How to convert a 2D array into a 1D array?"

        String[][] my2Darr = .....(something)......
        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);


回答4:

In Java 8 you can use object streams to map a matrix to vector.

Convert any-type & any-length object matrix to vector (array)

String[][] matrix = {
    {"a", "b", "c"},
    {"d", "e"},
    {"f"},
    {"g", "h", "i", "j"}
};

String[] array = Stream.of(matrix)
                       .flatMap(Stream::of)
                       .toArray(String[]::new);

If you are looking for int-specific way, I would go for:

int[][] matrix = {
    {1, 5, 2, 3, 4},
    {2, 4, 5, 2},
    {1, 2, 3, 4, 5, 6},
    {}
};

int[] array = Stream.of(matrix) //we start with a stream of objects Stream<int[]>
                    .flatMapToInt(IntStream::of) //we I'll map each int[] to IntStream
                    .toArray(); //we're now IntStream, just collect the ints to array.


回答5:

I'm not sure if you're trying to convert your 2D array into a 1D array (as your question states), or put the values from your 2D array into the ArrayList you have. I'll assume the first, but I'll quickly say all you'd need to do for the latter is call list.add(temp), although temp is actually unneeded in your current code.

If you're trying to have a 1D array, then the following code should suffice:

public static int mode(int[][] arr)
{
  int[] oneDArray = new int[arr.length * arr.length];
  for(int i = 0; i < arr.length; i ++)
  {
    for(int s = 0; s < arr.length; s ++)
    {
      oneDArray[(i * arr.length) + s] = arr[i][s];
    }
  }
}


回答6:

import java.util.*;

public class Main {

public static int A[][] = new int[3][3];
public static int B[] = new int[9];




public static void main(String[] args) {


    int temo = 0,t;

    Scanner s = new Scanner(System.in);

     System.out.println("Enter No for Matrix A");

     for (int row = 0; row < A.length; row++) {
            for (int col = 0; col < A.length; col++) {
                A[row][col] = s.nextInt();
            }
            System.out.print("\n");
        }


     for (int row = 0; row < A.length; row++) {
            for (int col = 0; col < A.length; col++) {
                    t= A[row][col];
                    B[temo]= t;
                    temo++;

            }
            System.out.print("\n");
        }

     System.out.print("After Converted to one d \n");
     for(int i =0;i<B.length;i++) {
         System.out.print(" "+B[i]+" ");
     }

}

}


标签: java arrays