just a plain copy/paste?
this would do that, assuming that both arrays are with a properly initial size.
for(int i = 0; i < arr.length; i++)
for(int j = 0; j < arr[i].length; j++)
lk[i][j] = arr[i][j];
Hey bro
this is my code:
public static void ltab(int[][] lk, int[][] arr, int nRows)
{
int r;
for (r=0; r<nRows; r++)
{
lk[r][0]=arr[r][2];
lk[r][1]=arr[r][1];
}
}
What I'm trying to do here is that pass data from colum nr 3 of arr[][] into the new column no 1 of lk[][]. It doesnt compile and the error it gives me is "INCOMPATIBLE TYPES"
here is my call in the main program:
newRows=A6Help.ltab(lk, arr, nRows);>
Use the java.util.Arrays class. It has a copyOf method that works pretty well.
Here is an example using a 2d array of ints.
import java.util.Arrays;
class TestArrayCopy
{
static int [][] a1 = { {1,2,3,4}, {2,4,6,8}, {3,6,9,12}, {4,8,12,16} };
public static void printTwoDArray(int [][] a)
{
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[0].length; j++)
{
System.out.print(a[i][j] + ((j==a[0].length-1)?"":", "));
}
System.out.println("");
}
}
public static void main(String [] args)
{
System.out.println("Original array");
printTwoDArray(a1);
int [][] b = Arrays.copyOf(a1, a1.length);
System.out.println("\nCopy of array");
printTwoDArray(b);
}
}
Here is one that uses a generic print method.
import java.util.Arrays;
class TestArrayCopy
{
static Integer [][] a1 = { {1,2,3,4}, {2,4,6,8}, {3,6,9,12}, {4,8,12,16} };
public static <T> void printTwoDArray(T [][] a)
{
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[0].length; j++)
{
System.out.print(a[i][j] + ((j==a[0].length-1)?"":", "));
}
System.out.println("");
}
}
public static void main(String [] args)
{
System.out.println("Original array");
printTwoDArray(a1);
Integer [][] b = Arrays.copyOf(a1, a1.length);
System.out.println("\nCopy of array");
printTwoDArray(b);
}
}
Hey rfencl
Thanks for the codes but I used a different method.
I read from my text file and passed the numbers into a new array. After that I entered a printing statement and I told the program what to print.
it is like:
int [][] lk= new int [50][3];
and a fantastic trick
for (i=0; i<nRows; i++)
{
System.out.println(lk[i][0]+ "\t" + lk[i][1]); //here I wrote what I need to print
}
}
How do you like that?>