How to add a 1D array to a 2D array? - java

Sorry first time asking a question here.
If I have a 2D Array like this:
int[][] array2d = {{1, 2, 3}, {6, 7, 8}};
How do I add multiple 1D Arrays like this:
int[] array1d = {3, 2, 1};
int[] array1d2 = {8, 7, 6};
so that my original 2d array becomes this:
int[][] array2d = {{1, 2, 3}, {6, 7, 8}, {3, 2, 1}, {8, 7, 6}};
Note: this is for adding information from a JTextfield into a JTable whenever a button is pressed. So, the 2d array will be used as the data inside the table. If there is a better way to accomplish this I would appreciate it too. =)

Your array :
int[][] array2d = {{1, 2, 3}, {6, 7, 8}};
is fixed in size, so you would have to create a copy with enough capacity to hold the new values:
int[][] newArray = Arrays.copyOf(array2d, 4);
newArray[2] = array1d;
newArray[3] = array1d2;
To add your data to the JTable the arrays would have to be first converted to a non-primitive type such as an Integer array. One option is to use the Apache Commons:
model.addRow(ArrayUtils.toObject(array));
for each row of the array.

arrays are fixed size so to append it you need to resize the array look at java.util.Arrays.
then set the arrays location
arra2d[index] = array1d;
is there are reason you are not using
TableModel.addRow(dataArray);
?

Related

ND4J set values using boolean indexing and computation

I want to set values in an array based on an index and using a computation. In numpy I would write the following:
array[array < 0] = 2 * array[array < 0]
Can something like this be achieved with ND4J as well?
Edit: And what about more complicated statements / indexing such as:
array[array2 < 0] = 2 * array3[array2 < 0]
(Assuming dimensions of array1, array2 and array3 match)
Conditional assign is what you would be the closest:
import org.nd4j.linalg.indexing.BooleanIndexing;
import org.nd4j.linalg.indexing.conditions.Conditions;
INDArray array1 = Nd4j.create(new double[] {1, 2, 3, 4, 5, 6, 7});
INDArray array2 = Nd4j.create(new double[] {7, 6, 5, 4, 3, 2, 1});
INDArray comp = Nd4j.create(new double[] {1, 2, 3, 4, 3, 2, 1});
BooleanIndexing.replaceWhere(array1, array2, Conditions.greaterThan(4));
There are quite a few conditions and things you can use in there. I would suggest using the second array and applying your arithmetic you want on a whole array and letting BooleanIndexing replace what you want selecting elements from the second array you pass in.

What's the advantages in these 2 types of declaration?

I have a question on the styles of declaration of arrays
//I get that you may just want to initliaze array with 0s for some reason
int[] myIntArray = new int[3];
//I get that you already know what values you want in this array
int[] myIntArray = {1, 2, 3};
What's the advantage in declaring in these 2 notations:
//Woulnd't I just use the 1st notation for this
int[] myIntArray;
myIntArray=new int[3];
//Wouldn't I just use the 2nd notation for this
int[] myIntArray = new int[]{1,2,3};
The 4th notation is almost as same as the 2nd notation except the fact that there are 2 references,myIntArray and an anonymous array, to the object {1,2,3} where the 2nd reference,anonymous is lost instantly
The difference between int[] myIntArray = {1, 2, 3}; and int[] myIntArray = new int[]{1,2,3}; is that the first syntax only works when initializing a variable.
So if you have code like this:
int[] myIntArray = {1, 2, 3};
// some code
if (someCondition) {
myIntArray= new int[] {4, 5, 6};
}
You can not replace the second one with just {4, 5, 6}, because that syntax is reserved for initializing only.
But new int[] {4, 5, 6} is a general expression that works basically anywhere.
It's mostly used where you want to construct an array and not assign it to a variable, such as directly passing it to a method call:
someFunctionTakingAnIntArray(new int[] {3, 4, 5});

Java multidimensional array layout

I am studying for an exam and this is about memory allocation of a multidimensional java array.
Given is the following code:
double [][] a = new double[4][];
for (int i = 0; i < 4; i++)
a[i] = new double[4-i];
I am supposed to draw the memory layout of this array, but I am afraid I don't fully comprehend how it even works.
It would also be very kind of you if you could show me how to print this array as list to the console so I can look at it. :)
Thank you for your time.
you don't have to create new array in the for loop. Try this:
double[][] a = new double[4][3];
Or you can initialize it in one statement like:
double[][] a = {
{1, 3, 2},
{4, 5, 6},
{7, 8, 9}
};
And then print:
System.out.println(Arrays.deepToString(a))
Since your array a is an array of array (2D) , you can use enhanced for loop to print elements.
So, your outer loop has double[] as type, and hence that declaration. If you iterate through your a in one more inner loop, you will get the type double.
double[][] a = {
{1, 3},
{4, 5},
{7, 8}
};
List<Double> dou = new ArrayList<Double>();
for (double[] k: a) {
for (double element: k) {
dou.add(element) ;
}
}
System.out.println(dou);
Output
[1.0, 3.0, 4.0, 5.0, 7.0, 8.0]
I am not sure if this answers your question.
Above picture depicts how array elements will be stored in memory.

Multidimensional array in Scala

Is it possible to create a multidimensional array in scala without using the "Array()"
Like this in java:
int[][] myIntArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
If i understood correctly, you dont want to declare the array repeating Array a lot of times.
You can try this:
val > = Array
val x: Array[Array[Int]] = >(
>(1, 2, 3),
>(4, 5, 6),
>(7, 8, 9)
)
Source (There are other suggestions also)

Inline Array Definition in Java

Sometimes I wish I could do this in Java:
for (int i : {1, 2, 3, 4, 5})
System.out.println(i);
Unfortunately, I have to do something like this instead:
int [] i = {1, 2, 3, 4, 5};
// ...
My recollection is that C++ has this sort of feature. Is there an OOP replacement for inline array definitions (maybe even to the point of instantiating anonymous classes)?
I think the closest you are gonna get is:
for(int i : new int[] {1,2,3,4})
You could create the int[] array in the for loop.
for (int i : new int[] {1, 2, 3, 4, 5}) {
...
}
Here you are making an anonymous int array, which is the closest thing to what you want. You could also loop through a Collection.
Note that this question has nothing to do with OOP. It's merely a matter of syntax. Java supports anonymous arrays/objects just like C++.
You don't have to create a separate variable for this. The syntax {1, 2, ...} is valid only for declarations, but you can always say new int[] {1, 2, ...}:
for (int i : new int[] {1, 2, 3, 4, 5})

Categories