Unable to think of a way to shift array - java

I am stuck and can't think of a way to properly shift an array by __ units. I am trying to create an array of 30 items (numbers 1-30) which can then be shifted to the right by the number the user inputs. This would mean that the first few numbers in the array would take the index's at the end of the array, and the rest of the numbers would be shifted to the left. (Ex, if shift = 3, numbers 1,2,3 would take the index of 27,28,29, and the rest of the numbers 4-30 would shift left making index 0 =4, index 1=5, index 2=6....
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("\nEnter the shift/rotation:");
int shiftNum = input.nextInt();
int [] numArray = new int [30];
for(int i = 0; i < 30; i++){
numArray [i] = i+1;
System.out.print(numArray[i]+" ");
}
}
}
This is the code I have so far, any suggestions to how I can do this? I have tried to make a separate for loop like
numArray [i-shiftNum] = numArray[i];
But when doing this, the index of 0-shiftNum would be negative and would not work. This is the context of the problem:
Create a program that will create an array of 30 items. Then it will rotate the array by a number selected by the user.

In order to shift the numbers in the array, the following for loop works for shifting the values within the array.
// prerequisite: array is already filled with values
for(int i = 0; i < numArray.length; i++) {
arr[i] += shiftNum;
if (numArray[i] > 30) {
numArray[i] -= 30;
} else if (numArray[i] <= 0) {
numArray[i] += 30;
}
}
According to you code, the array created will contain value from 1 - 30 including 1 and 30. If you want your code to contain values from 0 - 29 instead, change numArray[i] > 30 to numArray[i] >= 30 and change numArray[i] <= 0 to numArray[i] < 0.

Use Java's convenience methods. Most people still want to write for loops. Basically, you need to save off the elements you are overwriting with the shift. Then place those saved ones back in the array. System.arraycopy is nice in that it takes care of some nasty parts of moving elements in an array.
void shift(int shiftBy, int... array) {
int[] holdInts = Arrays.copyOf(array, shiftBy);
System.arraycopy(array, shiftBy, array, 0, array.length - shiftBy);
System.arraycopy(holdInts, 0, array, array.length - shiftBy, holdInts.length);
}

Here is quick fix for you. Please check following code.
Input :
Enter the shift/rotation: 4
Output :
Rotate given array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
After Rotate [27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
public static void main(String[] args) {
RotationDemo rd = new RotationDemo();
int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int k = 0;
Scanner scan = new Scanner (System.in);
try{
System.out.println("\nEnter the shift/rotation:");
int shiftNum = scan.nextInt();
if(shiftNum < 30) {
k = shiftNum;
System.out.println("Rotate given array " + Arrays.toString(input));
int[] rotatedArray = rd.rotateRight(input, input.length, k);
System.out.println("After Rotate " +
Arrays.toString(rotatedArray));
} else {
System.out.println("Shift number should be less than 30");
}
} catch(Exception ex){
} finally {
scan.close();
}
}
public int[] rotateRight(int[] input, int length, int numOfRotations) {
for (int i = 0; i < numOfRotations; i++) {
int temp = input[length - 1];
for (int j = length - 1; j > 0; j--) {
input[j] = input[j - 1];
}
input[0] = temp;
}
return input;
}
Hope this example works.

Related

How can I reorder my array based off the first value?

I'm working on a problem that says "Write a function called splay that reorganizes a list based on the first value. The first value is called the splaymaster. This function will rearrange the list such that all of the values before the splaymaster are less than or equal to the splaymaster and all of the values after it are greater than the splaymaster. The function also returns the index where the splaymaster is located after the list is rearranged. For example, if the list is [8, 15, 4, 48, 26, 45, 18, 29, 2, 1], the function will rearrange it to become [2, 1, 4, 8, 26, 45, 18, 29, 48, 15] and return the value 3. You may not sort the list." The problem that I seem to be having is for the example above it complains that the index is out of bounds which is my first problem. The next is if I use another array such as {90, 8, 15, 4, 48, 26, 45, 18, 29, 2, 1} I get {1, 90, 8, 15, 4, 48, 26, 45, 18, 29, 2} this is my output when it's not correct. What am I doing wrong? and How do I fix it?
public static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static int splay(int[] x) {
int left = 1;
int right = x.length;
int splaymaster = x[0];
while (left < right) {
if (x[left] <= splaymaster) {
left++;
} else {
swap(x, left, right);
}
swap(x, 0, left - 1);
}
return splaymaster;
}
}
int right = x.length;
The index of the last element in an array is x.length - 1, hence the index is out of bounds. Nonetheless, your algorithm seems incorrect. Merely swapping the elements at different locations is not sufficient. If an element is less than the splaymaster then you need to move all the elements up one and insert the smaller element into the array before the splaymaster. I assume there may be other conditions regarding the way to solve the problem but if there are then they are not clear to me from your question, for example it appears that the order of the elements is not important just as long as all elements before the splaymaster are less than or equal to it. I also assume you need to change the array in place, i.e. you are not allowed to use a second array.
Consider the following code:
import java.util.Arrays;
public class SplayTst {
public static int splay(int x[]) {
int index = 0;
int splaymaster = x[0];
for (int i = 1; i < x.length; i++) {
if (x[i] <= splaymaster) {
int temp = x[i];
for (int j = i; --j >= 0;) {
x[j + 1] = x[j];
}
x[0] = temp;
index++;
}
}
return index;
}
public static void main(String[] args) {
int[] test = new int[]{8, 15, 4, 48, 26, 45, 18, 29, 2, 1};
int ndx = splay(test);
System.out.println(Arrays.toString(test));
System.out.println(ndx);
test = new int[]{90, 8, 15, 4, 48, 26, 45, 18, 29, 2, 1};
ndx = splay(test);
System.out.println(Arrays.toString(test));
System.out.println(ndx);
}
}
Running the above code produces the following output:
[1, 2, 4, 8, 15, 48, 26, 45, 18, 29]
3
[1, 2, 29, 18, 45, 26, 48, 4, 15, 8, 90]
10
Alternatively, assuming that you can use any method to solve the problem, consider the following which uses ArrayList and then converts it to an array and then all the array elements are copied to the original array. Note that the single code line for converting ArrayList<Integer> to int[] is taken from the following question:
How to convert an ArrayList containing Integers to primitive int array?
I refer to this line of the below code:
int[] arr = list.stream().mapToInt(i -> i).toArray();
I iterate through the original array. If an element is greater than the splaymaster then it is appended to the ArrayList, otherwise it is inserted as the first element in the ArrayList.
import java.util.ArrayList;
import java.util.Arrays;
public class SplayTst {
public static int splay(int[] x) {
ArrayList<Integer> list = new ArrayList<>();
list.add(x[0]);
int index = 0;
for (int i = 1; i < x.length; i++) {
if (x[i] <= x[0]) {
list.add(0, x[i]);
index++;
}
else {
list.add(x[i]);
}
}
int[] arr = list.stream().mapToInt(i -> i).toArray();
for (int i = 0; i < x.length; i++) {
x[i] = arr[i];
}
return index;
}
public static void main(String[] args) {
int[] test = new int[]{8, 15, 4, 48, 26, 45, 18, 29, 2, 1};
int ndx = splay(test);
System.out.println(Arrays.toString(test));
System.out.println(ndx);
test = new int[]{90, 8, 15, 4, 48, 26, 45, 18, 29, 2, 1};
ndx = splay(test);
System.out.println(Arrays.toString(test));
System.out.println(ndx);
}
}

Move array element to index 0 and move elements left to right [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So the main idea is, the last number came on roulette machine is going to be on Index[0] and the element at index "whatNumberCame" has to become the number before
public class Main {
public static void main(String[] args) {
int[] rouletteNumbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number : ");
int whatNumberCame = scanner.nextInt();
int collisionIndex;
for(int i = 0; i < rouletteNumbers.length ; i++){
if(rouletteNumbers[i] == whatNumberCame){
System.out.println("COLLISION AT " + rouletteNumbers[i]);
collisionIndex = rouletteNumbers[i];
System.out.println(collisionIndex);
for (int j = collisionIndex + 1; j <= 0 ; j--){
rouletteNumbers[j] = rouletteNumbers[j - 1];
System.out.print(rouletteNumbers);
}
}
}
}
}
for example if i enter 10 array should become :
{10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}
You can tweak your code like this to make it works:
public static void main (String [] args) {
int[] rouletteNumbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number : ");
int whatNumberCame = scanner.nextInt();
for(int i = 0; i < rouletteNumbers.length ; i++){
if(rouletteNumbers[i] == whatNumberCame){
for (int j = i; j > 0; j--){
rouletteNumbers[j] = rouletteNumbers[j - 1];
}
rouletteNumbers[0] = whatNumberCame;
break;
}
}
System.out.println(Arrays.toString(rouletteNumbers));
}
In your loop, you used j <= 0 which is always false, so you inner loop doesn't execute at all.
And when you find the position of the number, you can break from the loop using break.
Here is a method that does the similar thing except it allows you to insert a value at whatever array index you like. With each value inserted into the array, the array will grow in length as indicated within your post example.
Usage:
To add a value to the array at index 0 (the beginning of the Array):
rouletteNumbers = insertIntoArray(rouletteNumbers, whatNumberCame, 0);
To add a value to the very end of the array:
rouletteNumbers = insertIntoArray(rouletteNumbers, whatNumberCame, rouletteNumbers.length);
To insert a value into any index within the array (say index 4):
rouletteNumbers = insertIntoArray(rouletteNumbers, whatNumberCame, 4);
The method code:
public static int[] insertIntoArray(int[] array, int valueToInsert, int insertIntoIndex) {
/* If the inertIntoIndex value is greater than the supplied Array Length
then the inertIntoIndex value is changed to the supplied Array Length.
This ensures that the valueToInsert will be place at the end of the
Array. */
if (insertIntoIndex > array.length) {
insertIntoIndex = array.length;
}
int[] tmp = new int[array.length + 1];
int idxCounter = 0;
int i = 0;
for (; i < array.length; i++) {
if (i == insertIntoIndex) {
tmp[idxCounter] = valueToInsert;
idxCounter++;
}
tmp[idxCounter] = array[i];
idxCounter++;
}
/* when at this point in code, the condition below
indicates that the desired insert index location
would be at the very end of the array. */
if (i == idxCounter) {
tmp[idxCounter] = valueToInsert;
}
return tmp;
}

How can I pick out the odd numbers and even numbers from a given array and then store them in another array?

How can I pick out the odd numbers and even numbers from a given array and then store them in another array? The flow is: the odd numbers will go to the odd[] array while the even numbers will go to the even[] array??
Here's my code, I'm not sure if this is correct since it somewhat stores and prints a mix of zeros and even numbers, no presence of odd numbers.....
int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int[] odd = new int[10];
int[] even = new int[10];
for (int i = 0; i < num.length; i++) { // For odd numbers
if (num[i] % 2 != 0) {
num[i] = odd[i];
}
System.out.println(num[i] + " ");
}
for (int j = 0; j < num.length; j++) { // For even numbers
if (num[j] % 2 == 0) {
num[j] = even[j];
}
System.out.println(num[j] + " ");
}
You can do all that in one loop - that would be way faster. To know the correct position to put the number in, add extra counter for each array.
Your kind of approach
int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int[] odd = new int[10];
int[] even = new int[10];
int oddPos = 0;
int evenPos = 0;
for (int i = 0; i < num.length; i++) {
if (num[i] % 2 == 0) {
even[evenPos] = num[i];
evenPos++;
} else {
odd[oddPos] = num[i];
oddPos++;
}
}
However this would not be the best solution as you (in most cases) cannot determine the length of odd and even arrays beforehand. Then you should use either arraylists or count the values of each or something else.
More dynamic approach
As stated before - you need to determine the size of the arrays at first
int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int oddCount = 0, evenCount = 0;
int oddPos = 0, evenPos = 0;
//get the count of each type
for (int i = 0; i < num.length; i++) {
if (num[i] % 2 == 0)
oddCount++;
else
evenCount++;
}
//define arrays in correct sizes
int[] odd = new int[oddCount];
int[] even = new int[evenCount];
//put values in arrays
for (int i = 0; i < num.length; i++) {
if (num[i] % 2 == 0) {
even[evenPos] = num[i];
evenPos++;
} else {
odd[oddPos] = num[i];
oddPos++;
}
}
the approach for detecting odd and even numbers is correct, But I think the problem with the code you wrote is that the length of odd and even arrays, isn't determinant. so for this matter, I suggest using ArrayList<Integer>, let's say you get the array in a function input, and want arrays in the output (I'll mix the arrays in the output for better performance. but separating the functions for each list extracting is also ok depending on what you're going to do with them).
Solution
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static Integer[][] separateOddnEven(int[] input) {
Integer[][] output = new Integer[2][];
List<Integer> odds = new ArrayList<>();
List<Integer> evens = new ArrayList<>();
for (int i = 0; i < input.length; ++i) {
int temp = input[i];
if (temp % 2 == 0)
evens.add(temp);
else
odds.add(temp);
}
// alternative is to use these Arraylists directly
output[0] = new Integer[odds.size()];
output[1] = new Integer[evens.size()];
output[0] = odds.toArray(output[0]);
output[1] = evens.toArray(output[1]);
return output; // index 0 has odd numbers and index 1 has even numbers.
}
public static void main(String[] args) {
int[] input = {0, 21, 24, 22, 14, 15, 16, 18};
Integer[][] output = separateOddnEven(input);
System.out.println("odd numbers :");
System.out.println(Arrays.toString(output[0]));
System.out.println("even numbers :");
System.out.println(Arrays.toString(output[1]));
}
}
output :
odd numbers :
[21, 15]
even numbers :
[0, 24, 22, 14, 16, 18]
You can collect a 2d array with two rows: even and odd as follows:
int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
// a 2d array of two rows: even and odd
int[][] arr = new int[2][];
// process a 1d array and fill a 2d array
Arrays.stream(num).boxed()
// Map<Integer,List<Integer>>
.collect(Collectors.toMap(
// key: 0 - even, 1 - odd
n -> n % 2,
// value - a list of one
// element, i.e. number
n -> new ArrayList<>(List.of(n)),
// merge duplicates
(list1, list2) -> {
list1.addAll(list2);
return list1;
}))
// fill the rows of a 2d array: even and odd
.forEach((key, value) -> arr[key] = value.stream()
.mapToInt(Integer::intValue).toArray());
// output
System.out.println("Even: " + Arrays.toString(arr[0]));
// Even: [2, 4, 6, 8, 10, 12, 14, 16]
System.out.println("Odd: " + Arrays.toString(arr[1]));
// Odd: [1, 3, 5, 7, 9, 11, 13, 15]
in lambda (3 lines)
int[] nums = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
separate even and odd nums with partitioningBy:
Map<Boolean, List<Integer>> map = IntStream.of(nums)
.boxed().collect(partitioningBy(n -> (n & 1) == 0));
…and transform the resulting List<Integer> for even and odd to int[]:
int[] even = map.get(true).stream().mapToInt(i -> i).toArray();
int[] odd = map.get(false).stream().mapToInt(i -> i).toArray();
System.out.println("even numbers: " + Arrays.toString(even));
System.out.println("odd numbers: " + Arrays.toString(odd));
even numbers: [2, 4, 6, 8, 10, 12, 14, 16]
odd numbers: [1, 3, 5, 7, 9, 11, 13, 15]

How to get range to first index when the loop reach end?

I've got List<List<Integer>> with several objects. Each of inner element contains randomly shuffled indexes, for example (it's only a part of indexes).
[1, 4, 5, 2, 0, 3, 6]
[4, 2, 5, 3, 1, 6, 0]
[0, 3, 6, 1, 2, 4, 5]
I've got also an array with some values (int[][] array)
And I need to do a loop for each element to get value from indexes and move forward by 1 index and when I reach last index I need to get value from this index and the first one. After that loop end and sum values. It might look difficult but pictrue will show what I mean. But I dont know how to do this (loop is required for each element, I'm gonna have a massive List of List<Integer> inside and every object gonna have multiple indexes.
I'm reading data from file and write it to array
List<String> result = new ArrayList<>();
int[][] array = new int[][]{};
try (Scanner sc = new Scanner(theFile)) {
while (sc.hasNext()) {
result.add(sc.nextLine());
}
int max = Integer.parseInt(result.get(0).trim());
array = new int[max][max];
for (int i = 1; i < result.size(); i++) {
String[] tmp = result.get(i).trim().split(" ");
for (int j = 0; j < tmp.length; j++) {
array[i - 1][j] = Integer.parseInt(tmp[j]);
array[j][i - 1] = array[i - 1][j];
}
}
List<List<Integer>> collectionWithSubjects = new ArrayList<>();
for (int i = 0; i < 40; i++) {
List<Integer> sub = new ArrayList<>();
sub = sequence(0,51);
Collections.shuffle(sub);
collectionWithSubjects.add(sub);
}
You've done a decent job of explaining the problem. It sounds like you're getting caught up on trying to do everything in a single for loop, rather than breaking down the problem into two pieces.
This is your statement:
I need to do a loop for each element to get value from indexes and move forward by 1 index and when I reach last index I need to get value from this index and the first one.
This can be divided into two pieces:
I need to do a loop for each element to get value from indexes and move forward by 1 index
This is a for loop that iterates from 0 -> size() - 1. If we go from 0 -> size() we get an overflow.
for(int i = 0; i < list.size() - 1; i++) {
int firstCoord = list.get(i);
int secondCoord = list.get(i+1);
//do stuff
}
when I reach last index I need to get value from this index and the first one.
This is getting the last element and the first element.
int firstCoord = list.get(list.size() - 1);
int secondCoord = list.get(0);
Combine both together and you've got the framework for getting the coordinates.
for(int i = 0; i < list.size() - 1; i++) {
int firstCoord = list.get(i);
int secondCoord = list.get(i+1);
//do stuff
}
int firstCoord = list.get(list.size() - 1);
int secondCoord = list.get(0);
//do stuff
I'll leave the actual implementation up to you.
// given array
int[][] array = [[31, 21, 34, 22, 67, 14, 41],
[17, 42, 31, 57, 26, 23, 52],
[5, 92, 52, 52, 31, 22, 62],
[17, 42, 31, 57, 26, 23, 52],
[5, 92, 52, 52, 31, 22, 62],
[31, 21, 34, 22, 67, 14, 41],
[5, 92, 52, 52, 31, 22, 62]];
// given list of lists of randomly-ordered indices
List<List<Integer>> indexList = Arrays.toList([
Arrays.toList([1, 4, 5, 2, 0, 3, 6]),
Arrays.toList([4, 2, 5, 3, 1, 6, 0]),
Arrays.toList([0, 3, 6, 1, 2, 4, 5])
]);
// first, create a place to store the sums corresponding to each random list
List<Integer> sums = new ArrayList<Integer>();
// iterate over each of the lists of random elements
for(List<Integer> randomIndices: indexList){
// create a running sum for this list
int randomSum = 0;
// iterate over each element of the randomized index list
for(int j = 0; j < randomIndices.size(); j++){
// read the current and next index (using modulo to wrap around)
current = randomIndices.get(j);
next = randomIndices.get((j + 1) % randomIndices.size());
// add the relevant index in array to the running sum
randomSum += array[current][next];
}
// add the recorded randomSum to the sums list.
// Its index is the same as the random list we just iterated over
sums.add(randomSum);
}
System.out.println(sums);
You may iterate over the alues of each List<Integer> then look them by pair (use % to go back at the beginning) and use them to index the 2d array
for (List<Integer> l : list) {
int sum = 0;
for (int i = 0; i < l.size(); i++) {
int p1 = l.get(i % l.size());
int p2 = l.get((i + 1) % l.size());
sum += values[p1][p2];
}
System.out.println(sum);
}
With this as initial data
List<List<Integer>> list = List.of(List.of(1, 4, 5, 2, 0, 3, 6), List.of(4, 2, 5, 3, 1, 6, 0));
int[][] values = new int[][]{new int[]{31, 21, 34, 22, 67, 14, 41}, new int[]{17, 42, 31, 57, 26, 23, 52}, new int[]{5, 92, 52, 52, 31, 22, 62},
new int[]{17, 42, 31, 57, 26, 23, 52}, new int[]{5, 92, 52, 52, 31, 22, 62}, new int[]{31, 21, 34, 22, 67, 14, 41}, new int[]{5, 92, 52, 52, 31, 22, 62},};
it'll print
253
262

Trying to make a double array, numbered 2 to 50

I'm trying to make a 5 by 10 table using a double array. The first box should be blank, then the rest numbered 2-50.
I have this so far but it is not working.
int array[][] = new int[5][10];
for(int row=1; row<5;row++){
for(int col=1;col<10;col++)
array[row][col] = row*col;}
System.out.println(array);
row * col cannot give you consecutive numbers from 2 to 50. And in your code, you are not just leaving the first box, but you are leaving out first row and first column completely.
You should run the loop normally from 0 to max. And for [0][0], don't print anything.
Also, for printing from 2 to 50, just have a count variable which starts with 2, and after printing it, increment it by 1.
Here's the modified code: -
int array[][] = new int[5][10];
int count = 2;
for(int row=0; row<5;row++){
for(int col=0;col<10;col++) {
if (row == 0 && col == 0) {
continue;
}
array[row][col] = count++;
}
}
for (int[] inner: array) {
System.out.println(Arrays.toString(inner));
}
OUTPUT : -
[0, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
[41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
NOTE: -
Since you want your first box to be blank, you can't use Arrays.toString here. You would have to use one more loop, and print your array in simple ways. And when your indices are [0][0], just sysout("");
The first box can't be blank... it could be zero, is that what you want?
Changes:
Use 0 indices, not 1 indices
You have to print the contents of the array manually, see where I print a comma below
row * col isn't the correct value. use row * 10 + col + 1
Try this:
int array[][] = new int[5][10];
for(int row=0; row<5;row++){
for(int col=0;col<10;col++) {
array[row][col] = row * 10 + col + 1;
if (array[row][col] < 2) {
System.out.print(" ");
} else {
System.out.print(array[row][col]);
}
if (col < 9) System.out.print(",");
}
System.out.println();
}
Output:
,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21,22,23,24,25,26,27,28,29,30
31,32,33,34,35,36,37,38,39,40
41,42,43,44,45,46,47,48,49,50

Categories