将数组传递给构造函数或方法的参数

时间:2014-02-02 18:19:08

标签: java

当两个数组传递给此构造函数时,如何访问每个数组的每个元素。我只打印出一个对象引用([I @ 1befab0])。 问候 乔

  public class ConstParameters {


public ConstParameters(){
    }
  public ConstParameters(Object ob1, Object ob2){
     System.out.println("this is a constructor");
       System.out.println(ob1+"\n"+ob2);

  } 
public static void main(String[] args) {
    // TODO Auto-generated method stub

}

 }

2 个答案:

答案 0 :(得分:0)

使用此

public ConstParameters(Array[] ob1, Array[] ob2){
     System.out.println("this is a constructor");
       System.out.println(ob1+"\n"+ob2);

  }

或向下翻转您的对象

int[] obj1= (int[])ob1;//but make sure it is castable otherwise it will throw classcast Exception

答案 1 :(得分:0)

您可以在Java中使用Arrays实用程序类:

Arrays.toString(Object[] arrayOfObjects);

这将为您提供数组的简单字符串表示。

正如其他人所说,你的参数似乎不是数组类型,所以你的构造函数应该是这样的:

public ConstParameters(Object[] ob1, Object[] ob2)