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

  今天做了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"));
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。