将对象数组扩展为对象的二维数组

huangapple go评论63阅读模式
英文:

Expanding Object array to an object 2d array

问题

我卡在一个问题上,需要我将一个 Object 数组扩展成一个二维 Object 数组。这是他们给我的默认代码。有人有任何线索吗?

Object[][] expand(Object[] array){

}

问题本身说:

编写一个函数,接受一个 Object[] 数组,其中数组中的每个值本身都是一个 Object[],并返回一个与输入相同值的 Object[][]。
提示:你需要进行一些类型转换。你可以在一行中完成这个任务。

英文:

I am stuck on a problem which requires me to expand an Object array into a 2D Object array. This is the default code they gave me. Does anyone have any clue?

 Object[][] expand(Object[] array){

 }

The question itself says:

> Write a function that takes in an Object[] array, where each value in
> array is itself an Object[] and returns an Object[][] with the same
> value as the input.
>
> Hint: You will need to do some typecasting/type conversion. You can do
> this in one single line.

答案1

得分: 0

以下是翻译好的部分:

"问题似乎很明确。您已经获得了一个2D数组,但内部数组以Object类对象的形式给出。因为Object是所有类的父类(即使数组在内部也是一个类)。您只需要进行类型转换并返回2D数组。

Object[][] expand(Object[] array){
    Object[][] result = new Object[array.length][];
    for(int i=0;i<array.length;i++){
        result[i] = (Object[])array[i];
    }
    return result;
}

注意:代码部分未进行翻译。"

英文:

The Question seems to be clear. You are given a 2d array already but just that the inner array is given in the form of Object class object. Because Object is is parent class for all the classes (even the array is a class internally). You just need to typecast and return the 2d array.

Object[][] expand(Object[] array){
      Object[][] result = new Object[array.length][];
      for(int i=0;i&lt;array.length;i++){
          result[i] = (Object[])array[i];
      }
      return result;
    }

huangapple
  • 本文由 发表于 2020年7月29日 20:38:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63153794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定