0

I am working on a java program. where I have taken an input string and I am putting each char from a string in a 4*4 matrix. If the input string length is small than 16 i.e 4*4 matrix, then I am adding padding '#' 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. I can't use a vector, set, map. So How can I code now?

here is some code. key=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();
    }
}
  • What do you think `char[][] matrix = new char[key][key];` does, if not giving you a new array of a size you are choosing? – Andy Turner Jul 24 '18 at 14:13
  • _I want to create a new array and put remaining chars into it_... Just create a new array, copy all the elements of old array into new one, and fill the rest. – zlakad Jul 24 '18 at 14:13
  • @zlakad or just create a big-enough array in the first place. – Andy Turner Jul 24 '18 at 14:14
  • @AndyTurner - I think they means what should they do if the length of `key` is reached and still wants to add more elements. How they should go about re-sizing the array. – Dan W Jul 24 '18 at 14:14
  • @DanW the length of the input is known in advance, so you know if you'll run out of space in advance. – Andy Turner Jul 24 '18 at 14:15
  • @AndyTurner, yep, preferred way. Something's wrong in OP's logic – zlakad Jul 24 '18 at 14:15
  • Please explain how you decide the matrix size for various input lengths. What happens if the length is 17? 24? 25? Your best bet is to measure the input string, then allocate a correctly-sized matrix in advance. –  Jul 24 '18 at 14:15
  • "I can't use a vector, set, map." - How about a `List`? – Jacob G. Jul 24 '18 at 14:17
  • what I want is to make a new array and not to increase the size. for eg., if input string length is 20 then I will have 2 matrices. –  Jul 24 '18 at 14:18
  • @ganeshpawar Your comment just added more blur in your question. Please, edit your question ASAP. – zlakad Jul 24 '18 at 14:21
  • I you want to use more than one array, then you will need a container (`ArrayList`?) to store the arrays... – Serge Ballesta Jul 24 '18 at 14:30
  • Is this https://stackoverflow.com/questions/1647260/java-dynamic-array-sizes what you want? – GhostCat Jul 24 '18 at 14:56

2 Answers2

0

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?

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

    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.

Travis
  • 71
  • 3
0

If understand you request correctly, if the string length is bigger than 16 you just create a new array, well how about making a list of array initilized at one array and if there are more than 16 chars just add an array to the list using your method that returns an array.

ilyes hamrouni
  • 148
  • 1
  • 12