A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张飞年 中级黑马   /  2012-9-24 20:24  /  2325 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张飞年 于 2012-9-24 20:28 编辑
  1. public class Testkuangjia{
  2.         public static void main(String[] args) throws Exception{
  3.                 InputStream is = new FileInputStream("d:\\1.txt");//配置文件里的路径要是类全名不然报 NoFound 异常
  4.                 Properties prop = new Properties();
  5.                 prop.load(is);
  6.                 is.close();
  7.                 String classname = prop.getProperty("classname");//这里最好还是写出来。不然会报NoPointerException空指针异常
  8.                 Collection collections = (Collection)Class.forName(classname).newInstance(); //<font color="red">这句现在又报ClassCastException转换异常,这是什么原因呢,半天没检查出哪 不对</font>
  9.                
  10.                 TestPoint tp = new TestPoint(3,4);
  11.                 TestPoint tp2 = new TestPoint(4,4);
  12.                 TestPoint tp3 = new TestPoint(5,4);
  13.                
  14.                 collections.add(tp3);
  15.                 collections.add(tp);
  16.                 collections.add(tp2);
  17.                
  18.                 System.out.println(collections.size());
  19.         }
  20. }
  21. //这里配置文件中写的是classname = java.util.HashMap
复制代码

12 个回复

倒序浏览
本帖最后由 覃宏海 于 2012-9-24 20:34 编辑

public class Testkuangjia{

        public static void main(String[] args) throws Exception{

                InputStream is = new FileInputStream("d:\\1.txt");//配置文件里的路径要是类全名不然报 NoFound 异常

                Properties prop = new Properties();

                prop.load(is);

                is.close();

                String classname = prop.getProperty("classname");//这里最好还是写出来。不然会报NoPointerException空指针异常

                Collection collections = (Collection)Class.forName(classname).newInstance(); //这句现在又报ClassCastException转换异常,这是什么原因呢,半天没检查出哪 不对
回复  你在forName的内容里加个双引号试试看行不行 因为我没有你的TestPoint类,所以也不能运行看看问题点在哪里

               

                TestPoint tp = new TestPoint(3,4);

                TestPoint tp2 = new TestPoint(4,4);

                TestPoint tp3 = new TestPoint(5,4);

               
                collections.add(tp3);

                collections.add(tp);

                collections.add(tp2);

               
                System.out.println(collections.size());

        }

}
回复 使用道具 举报
覃宏海 发表于 2012-9-24 20:33
public class Testkuangjia{

        public static void main(String[] args) throws Exception{

这是TestPoint类,已经加了classname的引号但还有问题,
  1. public class TestPoint {
  2.         private int x;
  3.         public int y;
  4.         public TestPoint(int x, int y) {
  5.                
  6.                 this.x = x;
  7.                 this.y = y;
  8.         }
  9. }
复制代码
回复 使用道具 举报
      String classname = prop.getProperty("classname");//这里最好还是写出来。不然会报NoPointerException空指针异常---->老兄啊,getProperty(String key)这个方法好像没有搞明白哈
//API解释:Searches for the property with the specified key in this property list.   在属性list中查找String key对应的属性。
    Collection collections = (Collection)Class.forName(classname).newInstance(); //<font color="red">这句现在又报ClassCastException转换异常,这是什么
//这里是classname问题,前面的那句代码的问题~

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 参与有奖

查看全部评分

回复 使用道具 举报
Collection collections = (Collection)Class.forName(classname).newInstance();报错的原因是正常的,因为Class.forName(classname).newInstance();返回是HashMap的实例对象,而
接口 Collection<E>
所有超级接口:
Iterable<E>
所有已知子接口:
BeanContext, BeanContextServices, BlockingDeque<E>, BlockingQueue<E>, Deque<E>, List<E>, NavigableSet<E>, Queue<E>, Set<E>, SortedSet<E>
所有已知实现类:
AbstractCollection, AbstractList, AbstractQueue, AbstractSequentialList, AbstractSet, ArrayBlockingQueue, ArrayDeque, ArrayList, AttributeList, BeanContextServicesSupport, BeanContextSupport, ConcurrentLinkedQueue, ConcurrentSkipListSet, CopyOnWriteArrayList, CopyOnWriteArraySet, DelayQueue, EnumSet, HashSet, JobStateReasons, LinkedBlockingDeque, LinkedBlockingQueue, LinkedHashSet, LinkedList, PriorityBlockingQueue, PriorityQueue, RoleList, RoleUnresolvedList, Stack, SynchronousQueue, TreeSet, Vector
所以Collection的子类没有HashMap,所以这样的不同数据类型,又不是继承关系的赋值是不正确的,
回复 使用道具 举报
张飞年 发表于 2012-9-24 21:00
这是TestPoint类,已经加了classname的引号但还有问题,

我看了,刚才那个地方是不用加“”号的,因为它已经是String类型的了!我觉得问题可能出在你的配置文件上!你看下你的配置文件是怎么写的!是写的className 还是classname  如果是className就把
String classname = prop.getProperty("classname");
这里的名字改一下试试
回复 使用道具 举报
覃宏海 发表于 2012-9-24 22:04
我看了,刚才那个地方是不用加“”号的,因为它已经是String类型的了!我觉得问题可能出在你的配置文件上 ...

看过 了,,一直是小写,,没有错。
回复 使用道具 举报
武庆东 发表于 2012-9-24 21:03
String classname = prop.getProperty("classname");//这里最好还是写出来。不然会报NoPointerExcept ...

嗯,,,,这个我还是没明白 ,不过老师我看也是这么写的。
回复 使用道具 举报
本帖最后由 张飞年 于 2012-9-24 22:17 编辑
柳彬 发表于 2012-9-24 21:32
Collection collections = (Collection)Class.forName(classname).newInstance();报错的原因是正常的,因为 ...

这里得用List集合,
回复 使用道具 举报
武庆东 发表于 2012-9-24 21:03
String classname = prop.getProperty("classname");//这里最好还是写出来。不然会报NoPointerExcept ...

对对对,,的确是这样。
回复 使用道具 举报
看来楼主需要系统的总结一下集合框架了,下面是小弟学习时的一点心得,与楼主分享一下,希望对楼主有帮助:
Collection
        |--List:有序,可重复,有索引。
                |--ArrayList:
                |--LinkedList:
        |--Set:无序,不可以重复。

List的具体的容器:
        
List:有序(元素存入集合的顺序和取出的顺序一致),元素都有索引。元素可以重复。
        |--ArrayList:底层的数据结构就是数组。线程不同步的。ArrayList替代了Vector。查询元素的速度非常快。
        |--LinkedList:底层的数据结构是链表。增删元素的速度非常快。不同步的。
        |--Vector:底层的数据结构就是数组。线程同步的。Vector无论查询和增删都巨慢。

可变长度数组的原理:
当元素超出数组长度,会产生一个新数组,
将原数组的数据复制到新数组中,再将新的元素添加到新数组中。
ArrayList,是按照原数组的50%延长。
Vector是按照原数组的100%延长。
        
Set接口中的方法和Collection中方法一致的。Set接口取出方式只有一种,迭代器。
        |--HashSet:底层数据结构是哈希表。线程是不同步的。
                                HashSet集合保证元素唯一性:通过元素的hashCode方法,和equals方法完成的。
                                当元素的hashCode值相同时,才继续判断元素的equals是否为true。
                                如果为true,那么视为相同元素不存。
                                如果为false,那么存储。
                                如果hashCode值不同,那么不判断equals。

        |--TreeSet:对Set集合中的元素的进行指定顺序的排序。是不同步的。

Map
        |--Hashtable:底层是哈希表数据结构,是线程同步的。不可以存储null键,null值。
        |--HashMap:底层是哈希表数据结构,是线程不同步的。可以存储null键,null值。替代了Hashtable.
        |--TreeMap: 底层是二叉树结构。可以对map集合中的键进行指定顺序的排序。

Map集合:
存储和Collection有着很大不同。

Collection一次存一个元素。
Map一次存一对元素。

Collection是单例集合。
Map是双列集合。

Map中的存储的一对元素:一个是键,一个是值,键与值之间有对应(映射)关系。
特点:要保证map集合中键的唯一性。

Collection是单列集合,可以理解为,里面存储的都是光棍。
Map是双列集合,可以理解为:里面存储的都是夫妻。

看完这些总结,楼主的题目中报错信息,就很容易的解决了,具体的操作如下:
/**
*
* @author zhang
* d:\\1.txt文件中存放的是classname=java.util.HashMap
*/
public class MapDemo{
         
      public static void main(String[] args) throws Exception{

               InputStream is = new FileInputStream("d:\\1.txt");//配置文件里的路径要是类全名不然报 NoFound 异常

               Properties prop = new Properties();

               prop.load(is);

               is.close();

               String classname = prop.getProperty("classname");//这里最好还是写出来。不然会报NoPointerException空指针异常
               
               //①java.util.HashMap实现的接口是Map,不是Collection,Collection是List和Set的顶层接口
               Map collections = (Map)Class.forName(classname).newInstance();
              //<f ont color="red">这句现在又报ClassCastException转换异常,这是什么原因呢,半天没检查出哪 不对</font>

               TestPoint tp = new TestPoint(3,4);

               TestPoint tp2 = new TestPoint(4,4);

               TestPoint tp3 = new TestPoint(5,4);

               collections.put("1",tp3);

               collections.put("2",tp);

               collections.put("3",tp2);

               System.out.println(collections.size());

        }

}
请注意①的说明。

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 参与有奖

查看全部评分

回复 使用道具 举报
public class Testkuangjia{

        public static void main(String[] args) throws Exception{

                InputStream is = new FileInputStream("d:\\1.txt");//配置文件里的路径要是类全名不然报 NoFound 异常
                  System.out.println("1111111111111");
                Properties prop = new Properties();

                prop.load(is);

                is.close();

                String classname = prop.getProperty("classname");//这里最好还是写出来。不然会报NoPointerException空指针异常
                 System.out.println(Class.forName(classname));------------------->你这样检查看一下什么地方出了问题!如果运行到了这里,那就看一下返回的是不是HashSet集合
                Collection collections = (Collection)Class.forName(classname).newInstance(); //<font color="red">这句现在又报ClassCastException转换异常,这是什么原因呢,半天没检查出哪 不对</font>

                            System.out.println("12222222222222222");     

                TestPoint tp = new TestPoint(3,4);

                TestPoint tp2 = new TestPoint(4,4);

                TestPoint tp3 = new TestPoint(5,4);

               
                collections.add(tp3);

                collections.add(tp);

                collections.add(tp2);

               
                System.out.println(collections.size());

        }

}

//这里配置文件中写的是classname = java.util.HashMap

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 参与有奖

查看全部评分

回复 使用道具 举报
我知道了  你用的是Map集合!集合用错了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马