Copy a 1*9 array into a 3*3 array - Java - java

I'm trying to copy a 1 by 9 array into a 3 by 3 array. I'm not sure why my output is just the number 1 moving across the array. Any help would be greatly appreciated.
public class arrayTest{
public static void main(String [] args)
{
int A [] = {1,2,3,4,5,6,7,8,9};
int B [][] = new int [3][3];
int i = 0, k, j;
for(k = 0; k < 3; k++)
for(j = 0; j < 3; j++ ){
B[k][j] = A[i];
k++;
}
for(k = 0; k < 3; k++)
{
for(j = 0; j< 3; j++)
System.out.print(B[k][j] + " ");
System.out.println();
}
}
}

You need to convert the 2D coordinates from the loop counters into an index into the 1D array:
int A [] = {1,2,3,4,5,6,7,8,9};
int B [][] = new int [3][3];
for (int k=0; k < 3; k++) {
for (int j=0; j < 3; j++) {
B[k][j] = A[k*3 + j];
}
}
System.out.println(Arrays.deepToString(B));
// [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Related

Please Tell Me What is Wrong in My Code?!! Need to convert a 2D array into a side by side 1D array

public static void main(String[] args) {
int[][] arr = {{2, 3, 4}, {3, 4, 5, 2}};
System.out.println(line(arr));
}
public static int[] line(int[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
size++;
}
}
int[] array = new int[size];
int place = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
array[place] = arr[i][j];
place++;
}
}
return array;
}
The eror I'm getting is
----jGRASP exec: java Problem
[I#15db9742
----jGRASP: operation complete.
Every object has a toString() method, and the default method is to display the object's class name representation, then "#" followed by its hashcode. So what you're seeing is the default toString() representation of an int array. To print the data in the array, you can use
System.out.println(java.util.Arrays.toString(line(arr)));
Or you can loop through the array with a for loop like this
int [] res = line(arr);
for(int i=0;i<res.length;i++){
System.out.println(res[i]);
}
import java.util.Arrays;
public class TEst {
public static void main(String[] args) {
int[][] arr = { { 2, 3, 4 }, { 3, 4, 5, 2 } };
System.out.println(Arrays.toString(line(arr)));
}
public static int[] line(int[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
size++;
}
}
int[] array = new int[size];
int place = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
array[place] = arr[i][j];
place++;
}
}
return array;`enter code here`
}
}
As far as i can see it is working:
public static void main(String[] args) {
int[][] arr = {{2, 3, 4}, {3, 4, 5, 2}};
int[] i = line(arr);
Arrays.stream(i).forEach(x -> System.out.println(x + ""));
}
public static int[] line(int[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
size++;
}
}
int[] array = new int[size];
int place = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
array[place] = arr[i][j];
place++;
}
}
return array;
}
the only thing is when you are writing out your result you get the object reference not the array in plain text.
2
3
4
3
4
5
2
Process finished with exit code 0

how to sort multidimensional Array in java logically?

i need to sort a multidimensional Array without going for any predefined method, it will be logically..
int arr[][]={{5,2,3},{2,8,5},{7,4,5}};
in the case of single dimensional Array,
int num[]={125,28,31,40,12};
for(int i=0;i<=num.length-1;i++){
for(int k=0;k<=num.length-2;k++){
if(num[k]>num[k+1]){
int temp=0;
temp=num[k];
num[k]=num[k+1];
num[k+1]=temp;
}
}
}
for(int s=0;s<=num.length-1;s++){
System.out.println(num[s]);
}
what would be in case of multidimensional array?
public static void main(String[] args) {
int u, arr[][] = {{1, 7, 2}, {9, 12}, {54, 25, 10}};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
for (int k = j + 1; k < arr[i].length; k++) {
if (arr[i][j] > arr[i][k]) {
u = arr[i][k];
arr[i][k] = arr[i][j];
arr[i][j] = u;
}
}
System.out.print(arr[i][j] + ",");
}
System.out.println(" ");
}
}

Programming Issue in my counting sort algo

Please let me know the issue i have in this short code. I tried to follow the algo from the book and convert it into a program but have done some error in the process. Its throwing arrayoutofbounds exception
Algo which i tried to follow is bellow:
countingsort(a,b,k)
1)let c[0..k] be a new array
2)for i =0 to k
3) c[i]=0;
4)for j=1 to a.length
5) c[a[j]]=c[a[j]]+1
6)for i = i to k
c[i] = c[i] + c[i-1]
7)for j=a.length downto 1
b[c[a[j]]]= a[j]
c[a[j]] = c[a[j]]-1
public class CountingSort {
public static void main(String[] args) {
int[] array_A = {6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2};
int[] array_B = new int[array_A.length];
int k = 6;
countingSort(array_A,array_B,k);
//System.out.println(array_B);
}
public static void countingSort(int[] A, int[] B, int k){
int[] C = new int[k+1];
for(int i = 0; i<=k; i++){
C[i] = 0;
}
for(int j = 0; j<A.length; j++){
C[A[j]] = C[A[j]] + 1;
}
for(int i = 1; i<=k; i++){
C[i] = C[i] + C[i-1];
}
for(int j = A.length-1; j>=1; j--){
B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] - 1;
}
System.out.println(Arrays.toString(B));
}
}
Error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
at importantPrograms.CountingSort.countingSort(CountingSort.java:22)
at importantPrograms.CountingSort.main(CountingSort.java:11)
Here is the modified code that worked for me.
import java.util.Arrays;
public class CountingSort {
public static void main(String[] args) {
int[] array_A = {6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2};
int[] array_B = new int[array_A.length];
int k = 6;
countingSort(array_A,array_B,k);
System.out.println(Arrays.toString(array_B));
}
public static void countingSort(int[] A, int[] B, int k){
int[] C = new int[k+1];
for(int i = 0; i<=k; i++){
C[i] = 0;
}
for(int j = 0; j<A.length; j++){
C[A[j]] ++;
}
for(int i = 1; i<=k; i++){
C[i] += C[i-1];
}
for(int j = A.length-1; j>=0; j--){
B[--C[A[j]]] = A[j];
}
}
}
Ideally, instead of hard-coding the max value (6), you would find the max programmatically and size your count array accordingly. Such as
In main method
countingSort(array_A,array_B,max(array_A));
Max method
public int max(int[] arr){
int m = 0;
for(int i : arr){
if(i>m){
m = i;
}
}
return m;
}
Change
C[A[j]] = C[A[j] + 1]; to C[A[j]] = C[A[j]] + 1; because you are supposed to count the number of particular number occurrence.
I have no clue what you are doing but there is only a little mistake in your second for loop.
Change j<A.length to j<C.length

How to populate a 2 dimension array?

I have N numbers, N=10, how to populate it into a 2-D array so it looks like this:
{{1 2 3 4 5 }
{6 7 8 9 10}}
Here's what I have so far:
int[][] a = new int[2][5]
for(int i=0; i<2; i++)
for(int j=0; j<5; j++){
a[i][j] = ?
}
}
Or this is not possible at all?
You can keep the number in a variable, and increase it after each iteration:
int i = 1;
for (...) {
for (...) {
a[i][j] = i++; // the ++ operator will increase it by one
}
}
Given your first array, a I think you want something like this
public static void main(String[] args) {
int[][] a = new int[2][5];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = j + (a[i].length * i) + 1;
}
}
System.out.println(Arrays.deepToString(a));
}
Output is
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
Either you can increment var and fill the array:
int var = 1;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
a[i][j] = var++;
}
}
Or you can use Scanner and take in some input from the user:
Scanner in = new Scanner(System.in);
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
a[i][j] = in.nextInt();
}
}
int[][] a = new int[2][5];
int x = 1;
for(int i=0; i<2; i++) {
for(int j=0; j<5; j++){
a[i][j] = x++;
}
}
I can't tell if the values are known at compile time or if you need something dynamic at runtime. The sample in the original question and the answers above seem to suggest that maybe you are just trying to initialize the array with values known at compile time and if that is the case, you can do something like this:
int[][] nums = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};

Convert 1D to 2D java.lang.ArrayIndexOutOfBoundsException

I try to convert 1D to 2D array, but I keep getting java.lang.ArrayIndexOutOfBoundsException, and I have tried whatever I could find on the stackoverflow or internet, but I do not understand why I have this issue?
public class Arrayto2DArray {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int[] a = {0,1, 6, 83, 4, 5, 12, 7};
int[][] b = new int[4][4];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j]=0;
System.out.print(b[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < b[i].length; j++) {
try{
b[i][j] = a[i+j*4];
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(b[i][j]);
}
System.out.println();
}
}
}
I kind of know why I get this error and it because of this line
b[i][j] = a[i+j*4];
but I cannot come up any formula better than this.
Consider the second for-loop
Lets say when i = 3 and j= 3
a[i+j*4] evaluates to a[15] which is out of the array
When you declare you 2-d array, you specified int[][] b = new int[4][];, meaning that the first inner for loop for (int j = 0; j < b[i].length; j++) should result in a NullPointerException since b[i].length has no predefined length. Before intering the inner for loop, you should define the size of each b[i] like b[i] = new int[somenumber]
In regards to convert the 1d loop to a 2d, you need to define the rule around spliting it into the 2-d array. Then accordingly the second for loop need modification
EDIT:You modified your code to have an int[4][4] array, which means you have 16 placeholders. Your 1-d array contain only 8 placeholders. It depends on how you want to sort the array, like it can be
b 0 1 2 3
0 0 1 6 83
1 4 5 12 7
2 0 0 0 0
3 0 0 0 0
or any other pattern
Assuming the length of 1-d array is 8 and the total index of 2-d array is 16, the following is more of a general solution:
public static void main(String[] args) {
// TODO code application logic here
int[] a = { 0, 1, 6, 83, 4, 5, 12, 7 };
int[][] b = new int[4][4];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j] = 0;
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
System.out.println("--------------------------");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < b[i].length; j++) {
try {
if ((j + i * 4) < a.length)
b[i][j] = a[j + i * 4];
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
}
What is wrong is your 2D array is a 4*4, so that means for each index, there will be 4 elements i.e. say values. So there would be a total of 16 values, so you would need a 1D array with 16 elements for the above code to work. Right now you have only 8 values in your 1D array.
I think your code should change like this
public class Arrayto2DArray {
public static void main(String[] args) {
// TODO code application logic here
int[] a = {0,1, 6, 83, 4, 5, 12, 7};
int[][] b = new int[4][2];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j]=0;
System.out.print(b[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < b[i].length; j++) {
try{
b[i][j] = a[i+j*4];
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}
Your correct formula should be like.
int[] a = {0,1, 6, 83, 4, 5, 12, 7};
int[][] b = new int[4][2];
int k=0;
for (int i = 0; i < b.length; i++) { // Column of 2D array
for (int j = 0; j < b[0].length; j++) { // Row of 2D array
b[i][j]= a[k++];
System.out.println(b[i][j]);
}
}
Edit:
For general case, If your row is fixed at 4 but column is not fixed than your 2D conversion formula should be like below.
int col = a.length / 4;
int remainder = a.length % 4;
if (remainder > 0) {
col = col + 1; // Get the correct column size.
}
int[][] b = new int[4][col];
int k = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < col; j++) {
if (k < a.length) {
b[i][j] = a[k];
System.out.println(b[i][j]);
}
k++;
}
}

Categories