首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将集合数组更改为随机大小的数组

如何将集合数组更改为随机大小的数组
EN

Stack Overflow用户
提问于 2013-10-27 09:28:24
回答 3查看 106关注 0票数 0

如何将集合大小的数组更改为用户选择的随机大小的数组。

下面是我如何设置一个大小的数组。我只是不知道如何转换成任意大小的……

代码语言:javascript
运行
复制
    import java.util.Scanner;

public class Project1a {

 public static void scanInfo()
 {

 Scanner input = new Scanner(System.in);

System.out.println("Enter number of rows: ");    
int rows = input.nextInt();

System.out.println("Enter number of columns: "); 
int columns = input.nextInt();

int[][] nums = new int[rows][columns];            

 static int[][] sample = nums;

}
   static int[][] results = new int[4][5];
   static int goodData = 1;

   public static void main(String[] args) {
      analyzeTable();
      printTable(results);
   } 

   static void analyzeTable() {
      int row=0;
      while (row < sample.length) {
         analyzeRow(row);
         row++;
      }
   }

   static void analyzeRow(int row) {
      int xCol = 0;
      int rCount = 0;
      while (xCol < sample[row].length) {
         rCount = analyzeCell(row,xCol);
         results[row][xCol] = rCount; // instead of print
         xCol++;
      }
   }

   static int analyzeCell(int row, int col) {
      int xCol = col;   // varies in the loop
      int runCount = 0;  // initialize counter
      int rowLen = sample[row].length;   // shorthand
      int hereData = sample[row][xCol];  // shorthand
      while (hereData == goodData && xCol < rowLen) {
         runCount++;
         xCol++;
         if (xCol < rowLen) { hereData = sample[row][xCol];} 
      }
      return runCount;
   }

  /*   Start Print Stuff */
  public static void printTable(int[][] aTable ) {
    for (int[] row : aTable) {
      printRow(row);
      System.out.println();
    }
  }
  public static void printRow(int[] aRow) {
    for (int cell  : aRow) {
      System.out.printf("%d ", cell);
    }
  }
}
  /*   End Print Stuff */

我现在只在第18行得到了一个错误:非法的表达式开始。我认为我没有正确定义变量。

EN

Stack Overflow用户

发布于 2013-10-27 09:34:16

您不能在Java中更改数组大小,尽管在处理多维数组时会变得复杂。我建议您熟悉一下Java Array docs

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19613711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档