简体   繁体   English

如果我的数组大小在Java中已满,如何动态创建数组的新实例

[英]How can I create a new instance of array dynamically if my array size is full in java

I am working on a java program. 我正在研究Java程序。 where I have taken an input string and I am putting each char from a string in a 4*4 matrix. 我取了一个输入字符串,然后将字符串中的每个字符放入4 * 4矩阵中。 If the input string length is small than 16 ie 4*4 matrix, then I am adding padding '#' char. 如果输入字符串的长度小于16,即4 * 4矩阵,那么我要添加填充'#'char。

But Now, suppose the input string length is more than 16 then I want to create a new array and put remaining chars into it. 但是现在,假设输入字符串的长度大于16,那么我想创建一个新数组并将剩余的字符放入其中。 I can't use a vector, set, map. 我不能使用向量,集合,地图。 So How can I code now? 那么,我现在该如何编码?

here is some code. 这是一些代码。 key=4. 键= 4。

 char[][] giveMeNewArray() {
    char[][] matrix = new char[key][key];
    return matrix;
 }

 void putCharIntoMatrix() {
    int counter = 0;
    char[][] myArray = giveMeNewArray();
    System.out.println("myArray: " + myArray);

    for (int i = 0; i < key; i++) {
        for (int j = 0; j < key; j++) {
            if (counter >= inputString.length()) {
                myArray[i][j] = '#';
            } else {
                myArray[i][j] = inputString.charAt(key * i + j);
            }
            counter++;
        }
    }

    for (int i = 0; i < key; i++) {
        for (int j = 0; j < key; j++) {
            System.out.print(myArray[i][j] + "  ");
        }
        System.out.println();
    }
}

So if I'm understanding this question correctly, you want to create a matrix to hold the characters of an input string, with a minimum size of 4*4? 因此,如果我正确理解了这个问题,您想创建一个矩阵来容纳输入字符串的字符,最小尺寸为4 * 4吗?

You're probably better off creating a proper matrix rather than expanding it: 您最好创建一个合适的矩阵,而不是扩展它:

Do you want your matrix to always be square? 您是否希望矩阵始终为正方形?

  1. Get the next-highest (self-inclusive) perfect square using Math.sqrt 使用Math.sqrt获得第二高(包含在内)的完美平方

     int lowRoot = (int)Math.sqrt(inString.length()); int root; if(lowRoot * lowRoot < inString.length()) root = lowRoot+1; else root = lowRoot; 
  2. Create your matrix scaled for your input, minimum four 创建按输入比例缩放的矩阵,至少四个

     int size = (root < 4) ? 4 : root; char[][] matrix = new char[size][size]; 

But if you really want to expand it, you can just create a new matrix of a greater size: 但是,如果您真的想扩展它,则可以创建一个更大的新矩阵:

char[][] newMatrix = new char[oldMatrix.length+1][oldMatrix[0].length+1];

And copy the old matrix into the new matrix 并将旧矩阵复制到新矩阵中

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

If you expand by one each time you'll do tons of expands, if you expand by more you might expand too far. 如果每次扩展一次,则将进行大量扩展;如果扩展更多,则可能会扩展得太远。

This is really inefficient versus just doing some math at the beginning. 与刚开始做一些数学运算相比,这确实效率很低。 Making a properly sized matrix from the start will save you a bunch of loops over your data and regularly having two matrices in memory. 从一开始就创建适当大小的矩阵将为您节省一堆数据循环,并定期在内存中存储两个矩阵。

如果您理解正确,那么如果字符串长度大于16,则只需创建一个新数组,然后在一个数组中初始化一个数组列表,如果字符数超过16个,则使用您的数组将一个数组添加到列表中返回数组的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM