Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class ArrayFiller {
- /**
- * This will fill a 2D array of any size with letters a-z
- * looping until it reaches the end of the array.
- *
- * @param arrayToFill the array that is going to be
- * filled with characters a-z
- */
- public void fillArray(char[][] arrayToFill) {
- //The array of characters with the letters to fill the array
- char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
- //Counter to keep track of the index of the alphabet array
- int index = 0;
- //Loops through all the empty spaces in the array and fills them
- for(int row = 0 ; row < arrayToFill.length; row++) {
- for(int col = 0; col < arrayToFill[0].length; col++) {
- //Reset the index if it's at the end of alphabet
- if(index == 26)
- index = 0;
- //Set the index in the array to fill to a letter at index
- arrayToFill[row][col] = alphabet[index];
- //increment index
- index++;
- }
- }
- //Print the array
- for(int row = 0 ; row < arrayToFill.length; row++) {
- for(int col = 0; col < arrayToFill[0].length; col++) {
- //Print the element at row, col with a comma
- System.out.print(arrayToFill[row][col] + ", ");
- }
- //Skips a line when the row is done
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement