请问ArrayList和HashMap有什么区别?最好能给出些例子说明,万分感谢!

解决方案 »

  1.   

    怎么没人回答啊?是今天面试被问到的问题,大家帮帮我.还有一个问题,就是使用Data source和DriverClass有什么区别?谢谢!
      

  2.   

    java2容器就分为2种,
    1collection ,包括List和Set, 一组独立的元素.
    2map,一组成对的"键值对"对象 !
      

  3.   

    能说具体些吗,或者是否还有更多的区别?还有一个问题,就是使用Data source和DriverClass有什么区别?谢谢!
      

  4.   

    1、ArrayList是线性结构,所以存储得线性的,也就是有先后顺序,HashMap是关系型的,存储通过键值。2、第一种是早期的一种获得数据库连接的方式,第二种是目前比较流行常用的一种通过数据源来得到数据库连接的方式。两者都是为了得到connection,区别在于实现的方式不同。
      

  5.   

    arraylist 就是列表,相当于动态数组,HashMap 键-值 对,可以通过键获得值
      

  6.   

    给个提示:
    ArrayList和Vector的区别,HashMap和Hashtable的区别
    答:就ArrayList与Vector主要从二方面来说.
    一.同步性:Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步的
    二.数据增长:当需要增长时,Vector默认增长为原来一培,而ArrayList却是原来的一半
    就HashMap与HashTable主要从三方面来说。
    一.历史原因:Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现
    二.同步性:Hashtable是线程安全的,也就是说是同步的,而HashMap是线程序不安全的,不是同步的
    三.值:只有HashMap可以让你将空值作为一个表的条目的key或value 
    解释一下同步的概念:
    同步即是指任一时刻保证只能有一个线程对当前资源进行操作。
    继承关系如下:
    Collection(接口)->List(接口)->ArrayList(类)
    Map(接口)->HashMap(类)
    给一个例子学习一下:
    import java.util.ArrayList;
    import java.util.List;
    public class TestList {
    /**
    * @param List 接口是一种可含有重复元素的,有序的收集,也称序列,用户可以控制序列中
    * @param List 元素插入的位置,可以排序。
    */
    public static void main(String[] args) {
    ArrayList h = new ArrayList();
    h.add("1st");
    h.add("2nd");
    h.add("2nd");
    h.add(new Integer(3));
    h.add(new Double(4.0));
    mi(h);
    }public static void mi(List i) {
    System.out.println(i);
    }
    }
      

  7.   

    ArrayList 和 HashMap 就好象 C语言里 数组 与 链表 的区别。
      

  8.   

    给两端代码参考一下:import java.util.*;
    class HashMapTest
    {
    public static void printElements(Collection c)
    {
    Iterator it=c.iterator();
    while(it.hasNext())
    {
    System.out.println(it.next());
    }
    }
    public static void main(String[] args)
    {
    HashMap hm=new HashMap();
    hm.put("one","zhangsan");
    hm.put("two","lisi");
    hm.put("three","wangwu");

    System.out.println(hm.get("one"));
    System.out.println(hm.get("two"));
    System.out.println(hm.get("three"));


    Set keys=hm.keySet();
    System.out.println("Key:");
    printElements(keys);

    Collection values=hm.values();
    System.out.println("Value:");
    printElements(values);

    Set entry=hm.entrySet();
    printElements(entry);
    Iterator it=entry.iterator();
    while(it.hasNext())
    {
    Map.Entry me=(Map.Entry)it.next();
    System.out.println(me.getKey()+":"+me.getValue());
    }

    }
    }==================================import java.util.*;
    class ArrayListTest
    {
    public static void printElements(Collection c)
    {
    Iterator it=c.iterator();
    while(it.hasNext())
    {
    System.out.println(it.next());
    }
    }
    public static void main(String[] args)
    {
    //ArrayList al=new ArrayList();
    /*al.add("winsun");
    al.add("weixin");
    al.add("mybole");*/
    /*al.add(new Point(3,3));
    al.add(new Point(2,2));
    al.add(new Point(4,4));*/

    /*for(int i=0;i<al.size();i++)
    {
    System.out.println(al.get(i));
    }*/
    /*System.out.println(al);
    Object[] objs=al.toArray();
    for(int i=0;i<objs.length;i++)
    {
    System.out.println(objs[i]);
    }
    List l=Arrays.asList(objs);
    System.out.println(l);
    printElements(al);*/
    //l.add("zhangsan");
    //Iterator it=al.iterator();
    /*Iterator it=l.iterator();
    it.next();
    it.remove();
    while(it.hasNext())
    {
    System.out.println(it.next());
    }*/
    Student s1=new Student(2,"zhangsan");
    Student s2=new Student(1,"lisi");
    Student s3=new Student(3,"wangwu");
    Student s4=new Student(2,"mybole");
    ArrayList al=new ArrayList();
    al.add(s1);
    al.add(s2);
    al.add(s3);
    al.add(s4);
    Collections.sort(al,Collections.reverseOrder());//new Student.StudentComparator());
    printElements(al);
    }
    }class Point
    {
    int x,y;
    Point(int x,int y)
    {
    this.x=x;
    this.y=y;
    }
    public String toString()
    {
    return "x="+x+","+"y="+y;
    }
    }class Student implements Comparable
    {
    int num;
    String name;
    static class StudentComparator implements Comparator
    {
    public int compare(Object o1,Object o2)
    {
    Student s1=(Student)o1;
    Student s2=(Student)o2;
    int result=s1.num > s2.num ? 1 : (s1.num==s2.num ? 0 : -1);
    if(result==0)
    {
    result=s1.name.compareTo(s2.name);
    }
    return result;
    }
    }
    Student(int num,String name)
    {
    this.num=num;
    this.name=name;
    }

    public int compareTo(Object o)
    {
    Student s=(Student)o;
    return num > s.num ? 1 : (num==s.num ? 0 : -1);
    }
    public String toString()
    {
    return num+":"+name;
    }
    }