从数组列递归获取字符串
我有一系列对象。每个数组元素的内容可以是字符串,也可以是另一个带有字符串的数组。或者它也可以是保存字符串的数组的数组。
示例:
Object obj1 = array[[str1, str2, str3], [str4, str5]]
or: Object obj2 =array [str1, str2]
or: Object obj3 = "this string"
我需要一个方法,该方法将此对象作为参数,如果它是前两种情况之一,则返回包含这些元素的单个数组。如果是最后一种情况,它将返回包含作为参数传入的单个字符串元素的数组。
所以,如果我这样做,
getDataForColumn(obj1) i get array: [str1, str2. str3....str5]
getDataForColumn(obj2) i get array: [str1, str2] //same as input, i know
getDataForColumn(obj3) i get array: ["this string"]
我会尝试,但我真的不知道如何用递归来做到这一点,也不可能,至少以这种方式。
这是我想到的,并被卡住了。
private static Object[] getDataForColumn(Object column) {
if(column instanceof Object[]){
Object[] castarray = (Object[])column;
Object[]newArray = new Object[castArray.length];
for (int i = 0; i < castarray.length; i++) {
if((castarray[i] instanceof Object[])){
//recursion, but how :D
}
else{
newArray[i] = castArray[i];
}
}
return newArray;
}
return new array with object that came in.....
}
请帮忙。 谢谢
I have an array of objects. Content of each array element can be String, or it can again be another array with strings. Or it can be again array of arrays that hold strings.
Example:
Object obj1 = array[[str1, str2, str3], [str4, str5]]
or: Object obj2 =array [str1, str2]
or: Object obj3 = "this string"
I need a method which takes this object as argument, and if it is one of first 2 cases, returns single array with those elements. And if it is a last case, it returns array with single string element that came in as param.
So, if i do
getDataForColumn(obj1) i get array: [str1, str2. str3....str5]
getDataForColumn(obj2) i get array: [str1, str2] //same as input, i know
getDataForColumn(obj3) i get array: ["this string"]
I am trying, but I really can not wrap my head how to do this with recursion, nor is it possible, well at least in this way.
This is what I came up with, and stuck.
private static Object[] getDataForColumn(Object column) {
if(column instanceof Object[]){
Object[] castarray = (Object[])column;
Object[]newArray = new Object[castArray.length];
for (int i = 0; i < castarray.length; i++) {
if((castarray[i] instanceof Object[])){
//recursion, but how :D
}
else{
newArray[i] = castArray[i];
}
}
return newArray;
}
return new array with object that came in.....
}
Please help.
Thanx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个简单的方法。我使用 List 因为我更喜欢在数组上使用它,但当然你可以制作一个包装器,最终将其转换为数组:
Here's a simple one to do it. I use List because I prefer it on arrays, but of course you can make a wrapper that will convert it to array in the end:
希望是你的意思。
尚未测试,但应该可以......
hope that what you meant.
didn't test it yet, but should work....
我建议使用 ArrayList 按顺序将所有展平数组添加在一起。您可以创建一个合并当前“结果”数组和递归调用返回的数组的数组,但使用 ArrayList 会更容易。这样您就不必迭代两个数组并将它们放在一个数组中。
我没有测试这个,但我认为这应该有效!
希望有帮助。
I recommend to use an ArrayList to sequentially add all the flattened arrays together. You could have created one Array merging the current "result"-Array and the Array returned by the recursive call but using the ArrayList makes it easier. This way you don't have to iterate both Arrays and put them together in one Array.
I did not test this but i think that should work!
Hope that helps.
是的,这是可能的,也许不是最好的方法。
我没有测试过,但我希望这会给你带来想法
yes it's possible, maybe not the best way.
I didn't test it, but I hope that will give you the idea