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

huangapple go评论68阅读模式
英文:

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

问题

我只是想尝试解决以下异常。

java.lang.ClassCastException: [Ljava.lang.Object;无法转换为[Ljava.lang.String;
请在这里帮助我。

我需要将其转换为List<String[]>。我应该如何处理?

List&lt;Object[]&gt; values = query.getResultList();
List&lt;String[]&gt; strings = new ArrayList&lt;&gt;();
for (Object[] object : values) {
    strings.add((String[]) object);
}

> values := [obj1,obj2,obj3]
>
> obj1 = "1","01";
> obj2 = "1","02";
> obj3 = "1:,"03";

英文:

I am simply trying to remove below exception.
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
Please help me out here.

I need to cast it as List<String[]>. How should i handle it?

  List&lt;Object[]&gt; values =  query.getResultList();
    List&lt;String[]&gt; strings = new ArrayList&lt;&gt;();
    for (Object[] object : values) {
        strings.add((String[]) object);
    }

> values : = [obj1,obj2,obj3]
>
> obj1 = "1", "01";
> obj2 = "1", "02";
> obj3 = "1:, "03";

答案1

得分: 0

我认为你应该使用 System.arraycopy

package com.lsk.backend;

import java.util.*;
import java.util.List;

/**
 * @ClassName : Test  //className
 * @Description :   //description
 * @Author : shikai.liu  //author
 * @Date: 2020-08-27 09:13  //Date
 */
public class Test {
    public static void main(String[] args) {
        List<Object[]> values = getResultList();
        List<String[]> strings = new ArrayList<>();
        for (Object[] object : values) {
            String[] dst = new String[object.length];
            System.arraycopy(object, 0, dst, 0, dst.length);
            strings.add(dst);
        }
        System.out.println(strings);
    }

    public static List<Object[]> getResultList() {
        Object[][] result = {
            {
                "0", "1"
            },
            {
                "0", "2"
            },
            {
                "0", "3"
            },
        };
        List<Object[]> ss = Arrays.asList(result);
        return ss;
    }
}
英文:

I think you should use the System.arraycopy

package com.lsk.backend;


import java.util.*;
import java.util.List;


/**
 * @ClassName : Test  //className
 * @Description :   //description
 * @Author : shikai.liu  //author
 * @Date: 2020-08-27 09:13  //Date
 */
public class Test {
    public static void main(String[] args) {
        List&lt;Object[]&gt; values =  getResultList();
        List&lt;String[]&gt; strings = new ArrayList&lt;&gt;();
        for (Object[] object : values) {
            String[]dst=new String[object.length];
            System.arraycopy(object,0,dst,0,dst.length);
            strings.add(dst);
        }
        System.out.println(strings);
    }
    public static List&lt;Object[]&gt;getResultList(){
        Object[][]result={
                {
                    &quot;0&quot;,&quot;1&quot;
                },
                {
                        &quot;0&quot;,&quot;2&quot;
                },
                {
                        &quot;0&quot;,&quot;3&quot;
                },
        };
        List&lt;Object[]&gt;ss=Arrays.asList(result);
        return ss;
    }

}

答案2

得分: 0

List<Object[]> values = query.getResultList();
List<String[]> strings = new ArrayList<>();
values.forEach(object -> {
   String[] stringArray = new String[object.length];
   System.arraycopy(object, 0, stringArray, 0, object.length);
   strings.add(stringArray);
});
英文:
List&lt;Object[]&gt; values =  query.getResultList();
List&lt;String[]&gt; strings = new ArrayList&lt;&gt;();
values.forEach(object -&gt; {
   String [] stringArray = new String[object.length];
   System.arraycopy(object, 0, stringArray, 0,object.length);
   strings.add(stringArray);
});

</details>



# 答案3
**得分**: 0

这对我最终起作用了

    values.forEach(o-> { strings.add(Arrays.copyOf(o, o.length, String[].class)); });

<details>
<summary>英文:</summary>

This worked for me finally     

    values.forEach(o-&gt;{ strings.add(Arrays.copyOf(o, o.length, String[].class));});



</details>



huangapple
  • 本文由 发表于 2020年9月9日 22:05:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63813408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定