0

I have written this code, but at run time I have this error:

[Ljava.lang.Object; cannot be cast to [[Ljava.lang.String; please help me, thanks!!!

public  java.util.List<String>  concatAll(java.util.List<java.util.List<String>> mergedList) {



    java.lang.String [][] mergedArray = (String[][])mergedList.toArray();

    Iterator<java.util.List<String>> itr = mergedList.iterator();  
    java.util.List<String> list1 = itr.next();

          java.lang.String [] firstArray = (String[])list1.toArray();           

        int totalLength = firstArray.length;
          for (String[] array : mergedArray) {
            totalLength += array.length;
          }
          String[] result = Arrays.copyOf(firstArray, totalLength);
          int offset = firstArray.length;
          for (String[] array : mergedArray) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
          }
          java.util.List<String> finalList = Arrays.asList(result);
          for (String list : finalList)
              System.out.println(list);

    return finalList;
}
2

5 Answers 5

3

mergedList.toArray() creates a singly indexed array typed as objects.

Each of the objects it contains is in fact a (singly-indexed) list of strings, though with this call syntax the type is not known at compile-time. It is not an array of strings, as would be needed for your cast to work.

Since your concatAll is trying to convert a List<List<String>> into a List<String> by some sort of concatenation operation, it may be best to do this without ever converting to a String[][] at all, but if you do want that conversion, it can be done as follows:

private String[][] toDoubleIndexArray(List<List<String>> mergedList) {
    String[][] result = new String[mergedList.size()][];
    for (int i = 0; i< mergedList.size(); i++) {
        List<String> currentList =  mergedList.get(i);
        result[i] = currentList.toArray(new String[currentList.size()]);
    }
    return result;
}

Original answer, not quite correct as noted by Xavi Lopez in comments:

Since mergedList has type List<List<String>>, mergedList.toArray() has type List<String>[], i.e., it's an array of lists, and not a doubly indexed array.

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

2 Comments

Wouldn't mergedList.toArray() be of type Object[] instead? And wouldn't mergedList.toArray(new List[0]) give a List[] ?
Actually, yes, you'd lose the type information. I'll update.
2

There's no out-of-the-box method, but it's fairly straightforward to do by hand:

// Create the outer dimension of the array, with the same size as the total list
String[][] mergedArray = new String[mergedList.size()][];

// Now iterate over each nested list and convert them into the String[]
// instances that form the inner dimension
for (int i = 0; i < mergedList.size(); i++) {
    mergedArray[i] = mergedList.get(i).toArray(new String[0]);
}

A slightly more efficient version of the loop body would be

List<String> innerList = mergedList.get(i);
String[] innerAsArray = innerList.toArray(new String[innerList.size()]);
mergedArray[i] = innerAsArray;

as this avoids the array resizing that would be required in my initial example (the new String[0] isn't large enough to hold the list elements). But quite frankly, unless this was a performance critical loop, I'd prefer the first version as I find it slightly clearer to see what's going on.

Comments

0

Hey you cannot convert the Multi dimentional String list to String array directly. Add the below code before trying to use the mergedArray:

/** Create Array **/     
String [][] mergedArray = new String[mergedList.size()][];

/** Initialize array from list **/  
for(int i=0; i< mergedList.size(); i++){
     mergedArray[i] = mergedList.get(i).toArray(new String[0]);
 }

This should do the trick

Comments

0

I think your return type is wrong if your intention is to return an array of array.

Try this:

public String[][] concatAll(java.util.List<java.util.List<String>> mergedList) {

    //java.lang.String [][] mergedArray = (String[][])mergedList.toArray();
    java.lang.String[][] mergedArray = new String[mergedList.size()][];

    Iterator<java.util.List<String>> itr = mergedList.iterator();
    int count = 0;
    while (itr.hasNext())
    {
        java.util.List<String> list1 = itr.next();
        String[] array1 = list1.toArray(new String[list1.size()]);
        mergedArray[count++] = array1;
    }


return mergedArray;
}

Comments

-2

You can't convert a

List<List<String>>

to a String[][] by using the out of the box toArray functionality. This would only work if you had:

List<String[]>. 

1 Comment

Is it possible to convert between a list of list string and a multi dimensional string array? Yes, Of course you can as long as you write some custom logic. You can't do it via toArray as u were trying though unless its a list of string array instead

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.