开发者

Recursively get strings from array columns

开发者 https://www.devze.com 2023-02-17 22:19 出处:网络
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 ofarrays that hold strings.

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;
        Ob开发者_Python百科ject[]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


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;
}


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....


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消