stackoverflow
stackoverflow

Reputation: 2224

How to get size of array returned by another method

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'

Upvotes: 1

Views: 117

Answers (3)

Chris Gong
Chris Gong

Reputation: 8229

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)

Upvotes: 1

singhakash
singhakash

Reputation: 7919

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

Upvotes: 1

KevinO
KevinO

Reputation: 4403

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]);
  }
}

Upvotes: 1

Related Questions