从数组列递归获取字符串

发布于 2024-10-23 12:18:47 字数 1207 浏览 1 评论 0原文

我有一系列对象。每个数组元素的内容可以是字符串,也可以是另一个带有字符串的数组。或者它也可以是保存字符串的数组的数组。

示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

末が日狂欢 2024-10-30 12:18:47

这是一个简单的方法。我使用 List 因为我更喜欢在数组上使用它,但当然你可以制作一个包装器,最终将其转换为数组:

public static List<String> flat(Object o)
{
    List<String> strings = new ArrayList<String>();
    if (o.getClass().isArray())
    {
        Object[] oarr = (Object[]) o;
        for (Object obj : oarr)
        {
            strings.addAll(flat(obj));
        }
    }
    else
    {
        strings.add(o.toString());
    }
    return strings;
}

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:

public static List<String> flat(Object o)
{
    List<String> strings = new ArrayList<String>();
    if (o.getClass().isArray())
    {
        Object[] oarr = (Object[]) o;
        for (Object obj : oarr)
        {
            strings.addAll(flat(obj));
        }
    }
    else
    {
        strings.add(o.toString());
    }
    return strings;
}
谁把谁当真 2024-10-30 12:18:47
getDataFromColumn(Object column) {
  List<String> list = new LinkedList<String>();
  recGetData(column,list);
  return list.toArray();
}
public void recGetData(Object column,List<String> list) {
  if (column instanceof String) {
     list.add((String)column);
  } else {
     for (Object o : (Object[])column) { 
      recGetData(o,list);
     }
  }
}

希望是你的意思。

尚未测试,但应该可以......

getDataFromColumn(Object column) {
  List<String> list = new LinkedList<String>();
  recGetData(column,list);
  return list.toArray();
}
public void recGetData(Object column,List<String> list) {
  if (column instanceof String) {
     list.add((String)column);
  } else {
     for (Object o : (Object[])column) { 
      recGetData(o,list);
     }
  }
}

hope that what you meant.

didn't test it yet, but should work....

苏璃陌 2024-10-30 12:18:47

我建议使用 ArrayList 按顺序将所有展平数组添加在一起。您可以创建一个合并当前“结果”数组和递归调用返回的数组的数组,但使用 ArrayList 会更容易。这样您就不必迭代两个数组并将它们放在一个数组中。

private static Object[] getDataForColumn(Object column) {
ArrayList results = new ArrayList();

if(column instanceof Object[]){

    Object[] castarray = (Object[])column;

    for (int i = 0; i < castarray.length; i++) {

          if((castarray[i] instanceof Object[])){
             results.addAll(Arrays.asList(getDataForColumn(castarray[i])))    
           }
           else{
             results.add(castarray[i]);    
           }
    }
    return results.toArray();
}

我没有测试这个,但我认为这应该有效!

希望有帮助。

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.

private static Object[] getDataForColumn(Object column) {
ArrayList results = new ArrayList();

if(column instanceof Object[]){

    Object[] castarray = (Object[])column;

    for (int i = 0; i < castarray.length; i++) {

          if((castarray[i] instanceof Object[])){
             results.addAll(Arrays.asList(getDataForColumn(castarray[i])))    
           }
           else{
             results.add(castarray[i]);    
           }
    }
    return results.toArray();
}

I did not test this but i think that should work!

Hope that helps.

恋竹姑娘 2024-10-30 12:18:47

是的,这是可能的,也许不是最好的方法。

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++) {
          newArray[i] = getDataForColumn(castarray[i]);
       }

    } else {
       String[] newArray = {column};
    }
    return newArray;
}

我没有测试过,但我希望这会给你带来想法

yes it's possible, maybe not the best way.

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++) {
          newArray[i] = getDataForColumn(castarray[i]);
       }

    } else {
       String[] newArray = {column};
    }
    return newArray;
}

I didn't test it, but I hope that will give you the idea

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文