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

go评论19阅读模式

Expanding Object array to an object 2d array

# 问题

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

}
``````

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

"问题似乎很明确。您已经获得了一个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;
}
``````

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

go 17

go 12

go 15

go 16