user1410223
user1410223

Reputation:

Java Mixing variable String[] with String in one only array of strings

Suppose this:

String s0 = "01234";
String[] S1 = {"jkl","abc","xyz"};
String[] S2 = {"OPQ","ghi","STU"};
String s3 = "56789";
get_AllStrings(s3, S1, S2, s0);

The returned String[] must be:

String[] NewArray = {"56789","OPQ","ghi","STU","01234"}

I want to obtain this strings like only one array of strings... Here my method:

public String[] get_AllStrings(String... argString) { //Only String or String[]
  int TheSize = 0;
  for (int i = 0; i<argString.length; i++) {
    if(argString[i]!= null && argString[i].getClass().isArray()) {
      String[] OneArray =  (String [])argString[i];
      TheSize += OneArray.length;
    } else {
      TheSize += 1;
    }
  }
  String[] NewArray = new String[TheSize];
  int ThePos = 0;
  for (int i = 0; i<argString.length; i++) {
    if(argString[i]!= null && argString[i].getClass().isArray()) {
      String[] OneArray = argString[i];
      System.arraycopy(OneArray, 0, NewArray, ThePos, OneArray.length);
      ThePos += OneArray.length;
    } else {
      String[] OneArray = {argString[i]};
      System.arraycopy(OneArray, 0, NewArray, ThePos, 1);
      ThePos += OneArray.length;
    }
  }
  return NewArray;
}

But, is not working...

Upvotes: 1

Views: 521

Answers (4)

Bohemian
Bohemian

Reputation: 425033

You can't pass a String[] into an element of a varargs String... parameter.

The only way to accept either String or String[] is a "typeless" varargs Object..., because Object is the only common type to both.

public static String[] get_AllStrings(Object... args) {
    ArrayList<String> result = new ArrayList<String>();
    for (Object o : args) {
        if (o instanceof String) {
            result.add((String)o);
        } else if (o instanceof String[]) {
            result.addAll(Arrays.asList((String[])o));
        } else {
            throw new IllegalArgumentException();
        }
    }
    return (String[])result.toArray();
}

Upvotes: 0

Leliel
Leliel

Reputation: 156

unfortunately, you're running up against Java's type system here.

String and String[] are not subtypes. so a variable, or array can only hold one or the other. Using object, as done by @Johan Henriksson throws away any type safety assurances from the compiler, since any object can be put in the array. this is okay if you have some garuantee that you'll only ever have Strings, and you'll need to cast to string on pulling out of the collection.

I'm not sure exactly what you're trying to achieve here So I'm not sure how to go about resolving this.

if you just want all the strings from all sources in a collection of some sort, I'd use a list it's unclear where you're getting these strings and string arrays from however.

Upvotes: 0

Hans Z
Hans Z

Reputation: 4744

What you want to do is to use an ArrayList instead of an array.

public static String[] getAllStrings(Object ... argString) {
    ArrayList<String> list = new ArrayList<String>();
    for (Object stringOrArray : argString) {
        if (stringOrArray instanceof String)
            list.add((String) stringOrArray);
        else {
            String[] strings = (String) stringOrArray;
            list.addAll(Arrays.asList(strings));
        }
    }
    return list.toArray(new String[list.size()]);
}

Upvotes: 2

Johan Henriksson
Johan Henriksson

Reputation: 687

I changed your code a bit and got this:

public static String[] get_AllStrings(Object... argString) {
      ArrayList<String> strings = new ArrayList<String>();
      for (int i = 0; i<argString.length; i++) {
        if(argString[i]!= null && argString[i].getClass().isArray()) {
          String[] OneArray =  (String [])argString[i];
          for(String str : OneArray)
              strings.add(str);
        } else {
            strings.add((String)argString[i]);
        }
      }
      return (String[])strings.toArray();
    }

I could not get it to accept both String and String[] with your method signature, but a change to Object... did the trick. You can use an ArrayList to create the array directly instead of looping through everything twice.

Upvotes: 0

Related Questions