在某教学视频中见到这样的代码,private Object items;
if(items instanceof Object[]){
//Object obj[]= items;一开始是这个的,eclipse上是提示类型不能转换。
Object obj[]= (Object[]) items;
                        //这里的编码是直接从object强制了,编译没有报错了,但运行时就出了问题
                        //这里想问下,可以强转Object类型成Object[]吗?
                        //为什么我看视频的时候,视频中强转运行也没问题呢?
this.collection=Arrays.asList(obj);
}
Type mismatch: cannot convert from Object to Object[]编码java类型强转

解决方案 »

  1.   

    这里想问下,可以强转Object类型成Object[]吗?
    --关于这个问题,可以肯定的说,一定条件下是可以的。条件就是,传参的items本身就是个Object[]数组时。因为,“万物皆对象”。视频中的没问题,就要仔细看看,他的和你的 有啥区别了。
      

  2.   

    按楼主的写法
    items instanceof Object[]应该是false
    怎么会执行到Object obj[]= (Object[]) items;这句呢?
      

  3.   

    一个命名为obj[]的Object,和一个类型是Object[] 的obj貌似不一样吧
      

  4.   

    Object item[]是数组对象 Object类型,所以前后来行保持一致的话 应该是Object obj[]= (Object) items; 
      

  5.   


    但视频上就是可以的。这里是把所有各种可能的类型都转成Object形式统一了。
      

  6.   


    但视频上就是可以的。这里是把所有各种可能的类型都转成Object形式统一了。
    不可能,你把视频中完整源代码发上来看看!
      

  7.   

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    public class ObjectDemo {
    public static void main(String[] args) {
     List<Object> collection = new ArrayList<Object>();
     Object  items = null;
    if(items instanceof Object[]){
    Object[] obj =(Object[]) items; 
    collection = Arrays.asList(obj);
    }
    }
    上面代码运行过没有问题, 那么Object[]就是数组对象object类型。  要不你把源码发我邮箱 我再参谋下。[email protected] 1165750988qq.com
      

  8.   

    我这个主要写的是模仿JSTL标签库中的forEach标签。//标签处理器
    package cn.junxu.web.example;import java.io.IOException;
    import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Arrays;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.Map;import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    public class ForEachTag2 extends SimpleTagSupport {
    @SuppressWarnings("unused")
    private Object items;
    private String var;
    @SuppressWarnings("rawtypes")
    private Collection collection;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void setItems(Object items) {
    this.items = items;

    if(items instanceof Collection){

    this.collection=(Collection) items;
    }

    if(items instanceof Map){
    Map map=(Map) items;
    this.collection=map.entrySet();
    }

    if(items.getClass().isArray()){
    this.collection=new ArrayList();

    for(int i=0;i<Array.getLength(items);i++){
    this.collection.add(Array.get(items, i));
    }

    }
    if(items instanceof Object[]){
    Object obj[]= (Object[]) items;
    this.collection=Arrays.asList(obj);
    }

    /* if(items instanceof int[]){
    this.collection=new ArrayList();
    int[] arr=(int[]) items;
    for(int i:arr){
    // for(int i=0;i<arr.length;i++){
    this.collection.add(i);
    }
    }*/
    }



    public void setVar(String var) {
    this.var = var;
    }
    @Override
    public void doTag() throws JspException, IOException {
    @SuppressWarnings("rawtypes")
    Iterator iterator=this.collection.iterator();
    while(iterator.hasNext()){
    Object value= iterator.next();
    this.getJspContext().setAttribute(var, value);
    this.getJspBody().invoke(null);
    }


    }
    -------------------------------------------------------------------------------------///tld文件配置,example2.tld<?xml version="1.0" encoding="UTF-8"?><taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">
     <tlib-version>1.0</tlib-version>
    <!--  <jsp-version>1.2</jsp-version>- -->
     <short-name>c</short-name>
     <uri>choose/when/otherwise</uri>
     <tag>
            <name>Choose</name>    
            <tag-class>cn.junxu.web.example.ChooseTag</tag-class>
            <body-content>scriptless</body-content>      
           
               
     </tag>
    <tag>
            <name>When</name>    
            <tag-class>cn.junxu.web.example.WhenTag</tag-class>
            <body-content>scriptless</body-content>      
            <attribute>
            <name>test</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            </attribute>
     </tag>
    <tag>
            <name>foreach</name>    
            <tag-class>cn.junxu.web.example.ForEachTag</tag-class>
            <body-content>scriptless</body-content>      
            <attribute>
            <name>items</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            </attribute>
     </tag>
    <tag>
            <name>foreach2</name>    
            <tag-class>cn.junxu.web.example.ForEachTag2</tag-class>
            <body-content>scriptless</body-content>      
            <attribute>
            <name>items</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            </attribute>
     </tag>
    <tag>
            <name>Otherwise</name>    
            <tag-class>cn.junxu.web.example.OtherWiseTag</tag-class>
            <body-content>scriptless</body-content>      
     </tag>
    <tag>
            <name>htmlfilter</name>    
            <tag-class>cn.junxu.web.example.HtmlFilterTag</tag-class>
            <body-content>scriptless</body-content>      
     </tag></taglib>
    --------------------------------------------------------------------------
    //foreach2.jsp,jsp显示页面
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@taglib uri="choose/when/otherwise" prefix="c" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'foreach.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
        <%
        List list=new ArrayList();
        list.add(1);
        list.add(2);
        list.add(43);
        list.add(344);
        
        request.setAttribute("list", list);
         %>
         
        <c:foreach2 items="${list}" var="num">
        ${num }</c:foreach2>
        <br>----------------------------------------- <br>
        
        <%
         Map map=new HashMap();
         map.put("aaa", 1111);
         map.put("bbb", 2222);
         map.put("eeee", 444);
         map.put("ggg", 7777);
         map.put("sssss", 884);
         request.setAttribute("map", map);
         %>
         
         <c:foreach2 items="${map }" var="entry">
         ${entry.key }= ${entry.value }
         </c:foreach2>
        <br>----------------------------------------- <br>
    <%--        <%
        Object num[]=new Object[]{"1","3","5","6"};
        request.setAttribute("num",num);
         %>
         <c:foreach2 items="${num }"  var="i">
         ${i }</c:foreach2>
         
        <br>----------------------------------------- <br>
           --%>
      
        <%
        double[] arr={1,3,4.4,52.121};
        request.setAttribute("arr", arr);
         %>
         <c:foreach2 items="${arr }" var="j">${j };</c:foreach2>
         
           <br>----------------------------------------- <br>
         <c:htmlfilter>
         <a href="">diandian</a>
         </c:htmlfilter>
        
      </body>
    </html>