Ragged 2D int array pyramid with Java - java

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.

Related

How to create dynamic matrix 2d in java?

I want to create dynamic matrix 2d using loop in java. My code is
class Mat {
public static void main (String[] args) throws java.lang.Exception {
List<List<Integer>> group = new ArrayList<>();
List<Integer> single = new ArrayList<>();
for (int i=0; i < 3; i++){
for (int j=0; j < 3; j++){
single.add(i);
}
group.add(single);
}
group.remove(3);
System.out.println(group);
}
}
First question, how to create dynamic matrix 2D with loop? I want an output like [[0,1,2], [0,1,2], [0,1,2]] and the matrix value saved in the variable group.
Second question, after being saved in the variable group, how about if I want to remove list (number 3) in the variable? So, output is [[0,1,2], [0,1,2]].
Thanks.
For the rest of your code creates a list List<List<Integer>> by changing
single.add(i);
to
single = new ArrayList<>(); // reset every iteration
for (int j=0; j < 3; j++) {
single.add(j); // add 0,1,2
}
how if i want remove list (number 3) in the variable?
group.remove(2); //removes the element at index 2

Given two 2D arrays, add them into a third 2D array

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.

How to get column in 2-D array in java by Foreach loop?

I can get a row from a 2D array in java by foreach loop like :
int[][] array = new int[5][5]
for (int[] row : array) {
for (int c : row) {
}
}
But How can I get the column form 2D array by foreach loop ? Or is this possible to get column from 2D array by foreach loop ?
Thank you.
One alternative can be
int i =0;
for (int k : array[0]){
for (int[] row : array) {
System.out.println(row[i]);
}
i++;
}
It's not possible. You'll have to use the traditional for loop :
int[][] array = new int[5][5]
for (int j = 0; j < array[0].length; j++) {
for (int i = 0; i < array.length; i++) {
int current = array[i][j];
}
}
2D array is just a conceptual meaning. in fact 2d array is a combination of multiple one-dimensional array. Therefore you cannot access the columns without using a counter. even if you use for each loop you need a counter inside.
If you need to get all the columns then you can make a loop with number of column. But in this case all the rows should have the same number of columns (elements)
public static void main(String[] args)
{
int [][] yourArray = {{1,2,3,4,5,6},//sample 2d array with 6 rows and six columns
{1,2,3,4,5,6}, //this is actually a collection of 6 different 1d arrays
{1,2,3,4,5,6},
{1,2,3,4,5,6},
{1,2,3,4,5,6},
{1,2,3,4,5,6}};
int yourColumn = 3; //example of selected column (be careful columns start from 0)
for(int[] row: yourArray)
{
System.out.println(row[yourColumn]);
}
}

Appending to double Array method

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.

Covert 2D array to 1D array storing the 2D array position (col, row) and the value in a 1D array

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!

Categories