1

Consider the following Java code,

public class FirstClass{
 public static Object[][] firstMethod(){
   Random random=new Random();
   int a=random.nextInt(100);
   int b=random.nextInt(100);
   int n=0;
   Object[][] arrayObj=new Object[a][b];
   for(int i=0;i<a;i++)
     for(int j=0;j<b;j++){
        arrayObj[i][j]=++n;
     }
     return arrayObj;
  }
}
public class SecondClass{
 public void secondMethod(){
   Object[][] myObj=FirstClass.FirstMethod();
 }
}

How can I get dimensional length of 'myObj' from 'secondMethod()' and how to print 'myObj'

4
  • What do you even mean by the "size" of a 2D array? Do you want each dimensional length, the total number of buckets, or something else? Commented Apr 21, 2016 at 5:22
  • First you need to return myObj from secondMethod() then use length property of the array to get size. Commented Apr 21, 2016 at 5:22
  • You really should do `random.nextInt(100) + 1, else expect you may have an array size of 0 for one of the dimensions. Commented Apr 21, 2016 at 5:23
  • I want to get each dimensional length, updated the question @TimBiegeleisen Commented Apr 21, 2016 at 5:27

3 Answers 3

1

You have few problems in your code like there is no main method, you method name is wrong in method call FirstClass.FirstMethod(); case matters.

Here is the Code

import java.util.Random;
class FirstClass{
 public static Object[][] firstMethod(){
   Random random=new Random();
   int a=random.nextInt(100);
   int b=random.nextInt(100);
   int n=0;
   Object[][] arrayObj=new Object[a][b];
   for(int i=0;i<a;i++)
     for(int j=0;j<b;j++){
        arrayObj[i][j]=++n;
     }
     return arrayObj;
  }
}
class SecondClass{
 public void secondMethod(){
   Object[][] myObj=FirstClass.firstMethod();
    System.out.println(myObj.length);      // length of row
    System.out.println(myObj[0].length);  //length of first column
    System.out.println(java.util.Arrays.deepToString(myObj));  //will print 2d array

 }
 public static void main (String[] args){
 new SecondClass().secondMethod();
 }
}

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

To get the size, you may do the following, assuming a uniform allocation.

 public void secondMethod(){
   Object[][] myObj=FirstClass.FirstMethod();

   int a = myObj.length;
   int b = myObj[0].length;
 }

To print the array, there are many approaches, but given the tone of the question:

for (int i = 0; i < myObj.length; ++i) {
  for (int j = 0; j < myObj[i].length; ++j) {
     System.out.println(myObj[i][j]);
  }
}

Comments

1

When defining the length of a 2D array, there are technically 2 sizes here. One is the number of arrays in the 2D array, which is given by myObj.length. And the other is the number of elements in a specific array in the 2D array, which is accessible by myObj[0].length for example. This returns the number of elements in the first array in myObj. 0 can be any number, depending on which array you want to know how many elements is in it. We can use both these lengths to print out all the elements in the array.

for (int i = 0; i < myObj.length; i++) {
  for (int j = 0; j < myObj[i].length; j++) {
     System.out.println(myObj[i][j]);
  }
}

Or just to print out the sizes of each array,

for (int i = 0; i < myObj.length; i++) { 
    System.out.println(myObj[i].length);    
}

Or to put the dimensional lengths into another array

int[] dimLengths = new int[myObj.length];
for (int i = 0; i < myObj.length; i++) { 
    dimLengths[0] = myObj[i].length;   
}

Lastly, Object[][] myObj=FirstClass.FirstMethod(); should be

Object[][] myObj=FirstClass.firstMethod(); (lowercase f instead of uppercase in firstmethod)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.