码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构在Java中的用法(持续更新...)

时间:2015-05-13 10:37:00      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:编程

  今天做了Medallia公司的Java面试题,发现用惯了C/C++之后对Java感到异常地不适应,特别是对数据结构在Java中如何使用感到十分头疼,于是开始整理并练习Java API里头关于数据结构的使用方法。甲骨文的Java API对每一种数据结构只提供解释但没有提供相关的例子,很不方便,因为大多数时候我们都是通过读例子来学习用法,这也是我学C++觉得最有用的方法。

Vector

  甲骨文API:“The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.”

insert( )

import java.util.*;

public class Insert
{
    public static void main(String[] arguments)
    {
        Vector<Integer> vector = new Vector<Integer>();

        // insert
        for(int i = 0; i < 3; i = i+1)
        {
            vector.add(i); // append at the end
        }
        for(int i = 0; i < vector.size(); i = i+1)
        {
            System.out.println(vector.elementAt(i)); // print 0 1 2
        }
    }
}

remove( )

import java.util.*;

public class Remove
{
    public static void main(String[] arguments)
    {
        Vector<Integer> vector = new Vector<Integer>();

        // insert
        for(int i = 0; i < 6; i = i+1)
        {
            vector.add(i); // append at the end
        }

        // remove 
        vector.remove(3);
        for(int i = 0; i < vector.size(); i = i+1)
        {
            System.out.print(vector.elementAt(i)); // print 0 1 2 4 5
        }
        System.out.println();
    }
}

Linked List

  甲骨文API:“Doubly-linked list implementation of the List and Deque interfaces. Implements all optional list operations, and permits all elements (including null).”

import java.util.*;

public class Demo
{
    public static void main(String[] arguments)
    {
        LinkedList<Integer> linkedlist = new LinkedList<Integer>();

        linkedlist.add(2); // add 2 to the end of list
        linkedlist.addFirst(1); // add 1 to the head of list
        linkedlist.addLast(4); // add 4 to the end of list
        linkedlist.add(2, 3); // insert 3 at index 2

        for(int i = 0; i < linkedlist.size(); i = i+1)
        {
            System.out.println(linkedlist.get(i));
        }

        System.out.println();

        // remove
        linkedlist.remove( linkedlist.size()-1 );

        for(int i = 0; i < linkedlist.size(); i = i+1)
        {
            System.out.println(linkedlist.get(i)); // print 1 2 3
        }
        System.out.println();

        // search
        System.out.println( linkedlist.indexOf(1) ); // print 0
    }
}

Stack

  甲骨文API:
  The Stack class represents a last-in-first-out (LIFO) stack of objects.
  A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:

Deque<Integer> stack = new ArrayDeque<Integer>();
import java.util.*;

public class Demo
{
    public static void main(String[] arguments)
    {
        Stack stack = new Stack();
        //Deque<Integer> stack = new ArrayDeque<Integer>();

        // insert
        for(int i = 0; i < 3; i = i+1)
        {
            stack.push(i);
        }

        // delete
        while( !stack.isEmpty() )
        {
            System.out.println( stack.pop() ); // print 2 1 0
        }
    }
}

Queue

import java.util.*;

public class Demo
{
    public static void main(String[] arguments)
    {
        Queue<Integer> queue = new LinkedList<Integer>();

        // insert
        for(int i = 0; i < 3; i = i+1)
        {
            queue.add(i);
        }

        // delete
        while( queue.peek() != null )
        {
            System.out.println( queue.poll() ); // print 0 1 2
        }
    }
}

Map

import java.util.*;

public class Demo
{
    public static void main(String[] arguments)
    {
        Map<String, Integer> map = new HashMap<String, Integer>();

        // insert
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);

        Iterator< Map.Entry<String, Integer> > iter = map.entrySet().iterator();
        while( iter.hasNext() )
        {
            Map.Entry<String, Integer> pair = iter.next();
            System.out.println(pair.getKey() + " --> " + pair.getValue());
        }

        // remove
        System.out.println("Now removing " + map.remove("b"));
        iter = map.entrySet().iterator();
        while( iter.hasNext() )
        {
            Map.Entry<String, Integer> pair = iter.next();
            System.out.println(pair.getKey() + " --> " + pair.getValue());
        }

        // search
        System.out.println("The first element is " + map.get("a"));
    }
}

数据结构在Java中的用法(持续更新...)

标签:编程

原文地址:http://blog.csdn.net/quantum_bit/article/details/45675909

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!