0

I have the following code in Java, when I run it, it give some error:

String[] stringArray1 = {"1","2","3"};
String[] stringArray2 = {"1","3","5"};
String[] stringArray3 = {"1","4","6"};

public String[] getString(int age) {

        List<String> list = new ArrayList<String>();

        switch (age) {
            case 1:

                Collections.addAll(list, stringArray1);
                return composeStrings(list);

            case 2:

                Collections.addAll(list, stringArray2);
                return composeStrings(list);

            case 3:
                Collections.addAll(list, stringArray3);
                return composeStrings(list);

            default:
                return new String[]{"Not find", "Please specify the age"};
            }
    }

public String[] composeStrings(List<String> list) {

        list.add("added a new string");

        return (String[]) list.toArray();
}

but when I run it, it says line: return (String[]) list.toArray();

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

what is the problem of this? and how to fix it?

6
  • Why are you returning String[].. why not return Collections.unmodifiableList(list); Commented Feb 3, 2014 at 15:23
  • I need to get a string Commented Feb 3, 2014 at 15:26
  • 1
    This is very convoluted.. you create a List to just throw it away Commented Feb 3, 2014 at 15:27
  • Further reading on casting from Object[] to String[]: en.wikipedia.org/wiki/… Commented Feb 3, 2014 at 15:27
  • You also are not checking the result of addAll Commented Feb 3, 2014 at 15:29

2 Answers 2

5

Try:

public String[] composeStrings(List<String> list) {

    list.add("added a new string");

    return list.toArray(new String[list.size()]);
}

You can read the javadoc for toArray here

Sign up to request clarification or add additional context in comments.

1 Comment

Won't need the cast (String[]). Generic types are erased in the compiled code. By passing an object to toArray the component type String can be determined and returned. If the size fits, that new array is used as return value, after filling it.
0

You have to pass String[] as argument to toArray() method otherwise it will return you a Object[]

public static void main(String[] args) {

        String[] stringArray1 = { "1", "2", "3" };
        String[] stringArray2 = { "1", "3", "5" };
        String[] stringArray3 = { "1", "4", "6" };

        List<String> list = new ArrayList<String>();

        Collections.addAll(list, stringArray1);
        Collections.addAll(list, stringArray2);
        Collections.addAll(list, stringArray3);

        String[] finalArray = list.toArray(new String[list.size()]);
        for (String item : finalArray) {
            System.out.println(item);
        }

    }

1 Comment

I have added the example code as well, now you can see there is no casting at all.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.