I am going through everything that I learned over the course of this quarter and re-studying the topics that I feel I didn't truly grasp. One of them was the concept of adding a third 2D array that stores the information for a previous 2D array.
The specific statement of what I should know by the end of this quarter was:
"Given two 2D arrays, add them into a third 2D array"
Here are my attempts to do this:
public static void main (String[] args){
int [] [] arrayOneTwoD= {{5,4,3,2,1},{1,2,3,4,5}};
int [] [] arrayTwoTwoD={{9,8,7,6,5},{5,6,7,8,9}};
int [][] thirdArray= new int [4][4];
thirdArray(arrayOneTwoD,arrayTwoTwoD,thirdArray);
}
public static void thirdArray (int [][] arrayOneTwoD, int [][] arrayTwoTwoD, int [] []thirdArray){
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
thirdArray[i][j] = arrayOneTwoD[i][j] + arrayTwoTwoD[i][j];
}
}
}
I searched this topic on stack overflow a number of times but nothing comes up for printing into a third 2D array only from one single 2D array into another.
What am I doing wrong?
you should do something like this:
public static void main (String[] args){
int [][] arrayOneTwoD= {{5,4,3,2,1},{1,2,3,4,5}};
int [][] arrayTwoTwoD={{9,8,7,6,5},{5,6,7,8,9}};
int [][] thirdArray = thirdArray(arrayOneTwoD,arrayTwoTwoD);
}
public static int[][] thirdArray(int[][] arrayOneTwoD, int[][] arrayTwoTwoD){
// assuming both arrays have the same dimensions
int[][] thirdArray = new int[arrayOneTwoD.length][arrayOneTwoD[0].length];
for(int i = 0; i < arrayOneTwoD.length; ++i) {
for(int j = 0; j < arrayOneTwoD[0].length; ++j) {
thirdArray[i][j] = arrayOneTwoD[i][j] + arrayTwoTwoD[i][j];
}
}
return thirdArray;
}
You'll want to figure out the dimensions of the arrays you're adding instead of hard-coding a value when you initialize the 3rd 2D array. You'll also need these dimensions when you iterate in your for loop. If the two 2D arrays you're adding are different dimensions, the answer becomes a little more complicated.
Related
Write the code required to allocate a ragged 2-D int array such that the first row has space to store 1 value, the second row can store 2 values, the third row has space to store 3 values, etc. up until the 50th row which has space to store 50 values.
I know for the above question I have to essentially create a pyramid with a 2 dimensional array. I don't really know how to manipulate 2D arrays, any help will be great. This is my code thus far, not sure how to allocate space like the question above says:
import java.util.Arrays;
public class Ragged2D {
public static void main(String[] args) {
int[][] boo = new int[50][];
for(int i = 0; i < boo.length; i++){
for(int k = 0; k< boo[i].length; k++){
}
}
System.out.println(Arrays.toString(boo));
}
}
This is how you initialize a row of the 2D array:
public static void main(String[] args) {
int[][] boo = new int[50][];
for(int i = 0; i < boo.length; i++){
boo[i] = new int[i+1]; // initialize the i'th row to have i+1 elements
for(int k = 0; k< boo[i].length; k++){
boo[i][k] = ...
}
}
System.out.println(Arrays.deepToString(boo)); // this change is required to print 2D array
}
I guess this is what you need
int[][] boo = new int[50][];
for (int i=0;i<50;i++) {
boo[i] = new int[i+1];
}
This way boo[0] can contain 1 element (boo[0][0]), boo[1] can contain 2 elements (boo[0][0] and boo[0][1]) etc.
So, I have a method like this
public String[][] getArgs(){
And, I want it to get results out of a for loop:
for(int i = 0; i < length; i++){
But how do I append them to the array instead of just returning them?
Create a String[][] array inside your method, fill this array inside a loop (or in any other way) and return that array in the end.
If you are sure you want to have only one for loop (instead of two, typical for 2-dimensional array), ensure your loop will go through the number of examples equal to the number of fields in your String[][] array. Then you can calculate the double-dimension array indexes from your single loop-iterator, for example:
for(int i = 0; i < length; i++){
int a = i % numberOfCollumnsInOutput;
int b = i / numberOfCollumnsInOutput;
String[a][b] = sourceForYourData[i];
}
(Of course which array dimension you treat as collumns (and which to be rows) depends on yourself only.) However, it is much more typical to go through an n-dimensional array using n nested loops, like this (example for 2d array, like the one you want to output):
for(int i = 0; i < dimensionOne; i++){
for(int j = 0; j < dimensionTwo; j++){
array[i][j] = someData;
}
}
For your interest. A sample code according to Byakuya.
public String[][] getArgs(){
int row = 3;
int column =4;
String [][] args = new String[row][column];
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
args[i][j] = "*";
return args;
}
You can make a LinkedList from that array, and then append the elements to it, and then create a new array from it. If you are not sure i'll post some code.
I have managed to convert it to output the values from the 2D array, but have no idea how to get the position.
Here is my code:
public static int[] convert(int [][]twodarray)
{
int[] onedarray = new int[twodarray.length * twodarray.length];
for(int i = 0; i < twodarray.length; i ++)
{
for(int s = 0; s < twodarray.length; s ++)
{
onedarray[(i * twodarray.length) + s] = twodarray[i][s];
}
}
return onedarray;
}
public static int [] printonedarray(int [] onedarray)
{
System.out.print("onedarray: ");
for(int i = 0; i < onedarray.length; i++)
{
System.out.print(onedarray[i] + "\t");
}
System.out.println();
return onedarray;
}
assuming that your 2-d array is not a jagged array, than the original cordinates for A[i] should be A[i/x][i%x] where x is the original length of the least significant column your 2/d array
Okay, I am not really sure if I get you correclty. But I understand it this way:
You have a 2 dim. array and want to convert it to a 1 dim. array.
Therefore you want to ready the first coloumn and the first line.
Then you want to add this value at the forst position of the 1 dim. array.
Then you read the next row and want to add this value and so on.
If I am right I suggest an arrayList for your 1 dim array. Because you don't know how deep the coloumns are. And ArrayLists are dynamic. You can simply add an element and don't need to give a position.
Your code suggestion was pretty good and I just converted it to an ArrayList.
import java.util.ArrayList;
public class test
{
public static ArrayList<Integer> convert(int [][]twodarray)
{
ArrayList<Integer> onedarray = new ArrayList<Integer> ();
for(int i = 0; i < twodarray.length; i ++)
{
for(int s = 0; s < twodarray[i].length; s ++)
{
onedarray.add(twodarray[i][s]);
}
}
return onedarray;
}
public static ArrayList<Integer> printonedarray(ArrayList<Integer> onedarray)
{
System.out.print("onedarray: ");
for(int i = 0; i < onedarray.size(); i++)
{
System.out.print(onedarray.get(i) + "\t");
}
System.out.println();
return onedarray;
}
}
If I missed your question I am sorry for a "wrong" answer.
I hope it will help you!
I have this three-dimensional array named bands. I need to do 4 copies of it, so I can work in parallel with all of them.
int bands[][][] = new int[param][][];
I need the array to keep being a three dimensional array, as it is the input for some methods that needs an int [][][]
How could I do such copies? I was thinking about using an arrayList like this:
List<Integer[][][]> bandsList = new ArrayList<Integer[][][]>();
bandsList.add(bands);
but I get this error on the last line: The method add(Integer[][][]) in the type List<Integer[][][]> is not applicable for the arguments (int[][][])
so what should I do??
The errors is because int[][][] is not the same as Integer[][][].
int[][][] is an 3D array of primitive int.
Integer[][][] is an 3D array of object Integer, which is the wrapper class of int.
Well, technically a 3D array is an array of pointers to a 2D array, which is an array of pointers to a 1D array which is an array of primitives or pointers to objects.
Use List<int[][][]> bandsList = new ArrayList<int[][][]>(); instead.
Also note that
bandsList.add(bands);
bandsList.add(bands);
will simply add 2 pointers to the same array, changing one will also change the other.
You'll need to manually copy them:
int[][][] getCopy(int[][][] bands)
{
int[][][] newBands = new int[bands.length][][];
for (int i = 0; i < bands.length; i++)
{
newBands[i] = new int[bands[i].length];
for (int j = 0; j < bands[i].length; j++)
{
newBands[i][j] = new int[bands[i][j].length];
System.arraycopy(bands, 0, newBands, 0, bands[i][j].length))
}
}
return newBands;
}
// to add
bandsList.add(getCopy(bands));
in this way you aren't doing a copy of the array, you have to do this 4 times:
int cloneList[][][] = new int[param][..][..];
for(int i = 0; i<bands.length; i++) {
int arr1[][] = bands[i];
for(int j = 0; j<arr1.length; j++) {
int arr2[] = arr1[j];
for(int k = 0; k<arr2.length; k++) {
int x = arr2[k];
cloneList[i][j][k] = x;
}
}
}
So the problem I'm working on solving involves an array list of array list of integers
. What is known: The number of elements in each ArrayList of integers. What is NOT known: How many ArrayList of Integers there actually are. I need suggestions for an algorithm that would sum the (ordered) elements of these arrays in every combination possible OF the arrays. In order to clarify what I mean by this let me give an example:
AoA = [[1,0,1,0],[0,1,0,1],[1,1,1,1],[0,0,0,0]];
Sum the elements of AoA[0] + AoA[1]; AoA[0]+AoA[2]; AoA[0]+AoA[3]; AoA[1]+AoA[2]; AoA[1]+AoA[3]; AoA[2]+AoA[3];
(4 choose 2)
So if anyone could code this simple version I'd be grateful as I'm struggling to do it. If anyone could code the more complex example where there's an unknown number of arrays in the AoA (so N choose 2), you'd be my hero.
TL;DR/edit
I need an algorithm to take n-choose-2 arrays from an array of arrays; sum the arrays (e.g. [1,2,3] + [1,2,3] = [2,4,6]); put the add the new summed array into an array of arrays.
If the 2 is fixed then the easiest thing I can think about is just generating the new array with N*(N-1)/2 rows, one for each sum and then using two variables to iterate through the original array with something like:s
int c = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
for (int k = 0; k < M; k++) {
sums[c][k] = AoA[i][k] + AoA[j][k];
}
c++;
}
}
Here's what I've got, took me a while but this will do what I was looking for:
it takes each combination of the arrays (t1, t2, t3, t4) and adds their elements and returns whatever combinatorial you choose for n, (in this example i left it as 3).
If there's any more optimizations you can see please feel free to add it. I'm a perl guy so making this work at all in Java was a real task.
import java.util.ArrayList;
public class testnCk {
public static void main(String[] args) {
ArrayList<int[]> sums = new ArrayList<int[]>();
int [] t1 = {1,1,0,0};
int [] t2 = {1,0,0,1};
int [] t3 = {0,0,0,0};
int [] t4 = {0,0,1,1};
ArrayList<int[]> testing = new ArrayList<int[]>();
testing.add(t1);
testing.add(t2);
testing.add(t3);
testing.add(t4);
int n = 3;
int i = -1;
int[] array = new int[4];
ArrayList<int[]> whatever = nCk(testing, sums, array, i, n);
for (int[] test1 : whatever)
{
for (int j = 0; j < test1.length; j++) {
System.out.print(test1[j]);
}
System.out.println();
}
}
public static ArrayList<int[]> nCk (ArrayList<int[]> arrayOfDiffPatterns, ArrayList<int[]> solutions, int[] tempsums, int i, int n)
{
n--;
for (int j=i+1; j<arrayOfDiffPatterns.size(); j++){
int[] array = tempsums.clone();
for (int k=0; k<arrayOfDiffPatterns.get(0).length; k++){
array[k] += arrayOfDiffPatterns.get(j)[k];
}
if(n>0){
nCk(arrayOfDiffPatterns, solutions, array, j, n);
}
else{
solutions.add(array);
}
}
return solutions;
}
}