1

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

2
  • 2
    Varargs will not help you do this, really. Why aren't you using a List<String> if you want to concatenate these strings and lists of strings? Where are you getting them from, that they're not coming all as Strings or all as String[]s? Commented Sep 17, 2013 at 21:24
  • A varargs parameter, such as String... will always be an array, even if only one argument was passed. It looks like you're trying to pass both String and String[] Objects to your get_AllStrings method, that's just not going to work. Commented Sep 17, 2013 at 21:27

4 Answers 4

2

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()]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

This is pretty good, but I would use instaceof to check for String[] else String else throw an IAE, so at least it's typed at runtime
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.