Converting an ArrayList into a 2D Array

前端 未结 8 2185
北海茫月
北海茫月 2020-12-04 01:55

In Java how do you convert a ArrayList into a two dimensional array Object[][]?

From comments: I will describe you the problem with more details: an XML file inclu

相关标签:
8条回答
  • 2020-12-04 02:47

    Try this:

    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(..);
    list.add(..);
    list.add(..);
    list.add(..);
    list.add(..);
    list.add(..);
    int[][] a = new int[list.size()][list.size()];
        for(int i =0; i < list.size(); i++){
          for(int j =0; j <list.size(); j++){
            a[i][j]= list.get(j +( list.size() * i));
          }
      }
    
    0 讨论(0)
  • 2020-12-04 02:49
    public static String[][] convertListIntoArrayObj(List<TeamMenuSelected> possibilities) {
        int numberOfColums = 2;
        int numberOfRows = possibilities.size();
        String[][] values = new String[numberOfRows][numberOfColums];
    
        for(int x=0; x<possibilities.size(); x++) {
            TeamMenuSelected item = possibilities.get(x);
            values[x][0] = item.getTeamName();
            values[x][1] = item.getTeamCuisine();
        }
    
        return values;
    }
    
    0 讨论(0)
提交回复
热议问题