用于学习 Java 集合的 小例子(ArrayList,map)

原创 2012年03月26日 17:11:43
package com.itm.arraylist;

import java.util.Arrays;
import java.util.List;

public class ArrayList {

	/**
	 * @param args
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		String fruits[] = { "apple", "banana", "cherry", "apple" };

		// asList : 返回一个受指定数组支持的固定大小的列表。
		List fruitList = Arrays.asList(fruits);

		int size = fruitList.size();
		System.out.println("List size = " + size);

		String s = (String) fruitList.get(2);
		System.out.println("List element #2 = " + s);

		if (fruitList.contains("banana"))
			;
		System.out.println("fruitList contains banana");

	}

}
package com.itm.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class SymbolMap {

	/**
	 * @param args
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		
		// 首先创造一个 Map,并向里面 添加一些值。
		HashMap symbols = new HashMap();
		symbols.put(new Integer( 34), "Double quote");
		symbols.put(new Integer( 37), "Percent");
		symbols.put(new Integer( 38), "Ampersand");
		symbols.put(new Integer( 60), "Less than");
		symbols.put(new Integer( 62), "Greater than");
		symbols.put(new Integer(162), "Cent");
		symbols.put(new Integer(163), "Pound");
		symbols.put(new Integer(169), "Copyright");
		symbols.put(new Integer(247), "Divide");
		
		if(args.length == 0){
			showMap(symbols);
		}else{
			int key = Integer.parseInt(args[0]);
			String value = (String) symbols.get(new Integer(key));
			if(value == null){
				System.out.println(key + " not in symbols");
			}else{
				System.out.println(key + " == " + value);
			}
		}

	}

	public static void showMap(Map map) {

		// 根据 遍历  取出 map里面的值。
		Iterator I = map.entrySet().iterator();
		while(I.hasNext()){
			Map.Entry entry = (Entry) I.next();
			System.out.println(
					entry.getKey() + ":" + entry.getValue()
			);
		}
	}

}





package com.itm.arraylist;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections; // plural!

class ArrayListDemo {

	// Display a List of objects
	public static void showContainer(List c) {
		for (int i = 0; i < c.size(); i++)
			System.out.println(c.get(i).toString());
	}

	public static void main(String args[]) {
		// Construct the container
		ArrayList charts = new ArrayList();

		// Insert some Data objects
		charts.add(new Chart(11013, "Morehead City Hrbr ", 12500));
		charts.add(new Chart(11552, "Neuse River        ", 40000));
		charts.add(new Chart(11428, "Dry Tortugas       ", 30000));
		charts.add(new Chart(11420, "Havana to Tampa Bay", 470940));
		charts.add(new Chart(25641, "Virgin Islands     ", 100000));
		charts.add(new Chart(26341, "Bermuda Islands    ", 50000));

		// Sort and display container
		Collections.sort(charts);
		showContainer(charts);
	}
}

===================================================================


package com.itm.arraylist;

import java.util.Comparator;

@SuppressWarnings("unchecked")
public class Chart implements Comparable {
	// Constants
	final static int BYNUMBER = 1;
	final static int BYNAME = 2;
	final static int BYSCALE = 3;

	public int number;
	public String name;
	public long scale;

	public Chart(int number, String name, long scale) {
		this.number = number;
		this.name = name;
		this.scale = scale;
	}

	public int compareTo(Object o) {
		Chart other = (Chart) o;
		return name.compareTo(other.name);
	}

	public String toString() {
		return number + "  " + name + "  1:" + scale;
	}

	// Comparator "factory" methods
	public static final Comparator byNumber() {
		return new ChartComparator(BYNUMBER);
	}

	public static final Comparator byName() {
		return new ChartComparator(BYNAME);
	}

	public static final Comparator byScale() {
		return new ChartComparator(BYSCALE);
	}

	// Private inner Comparator class
	private static class ChartComparator implements Comparator {

		private int compType; // Type of comparison to perform

		// Constructor saves comparison type identifier
		ChartComparator(int compType) {
			this.compType = compType; // BYNUMBER, BYNAME, or BYSCALE
		}

		// Implement the Comparator interface's method
		public int compare(Object o1, Object o2) {
			Chart c1 = (Chart) o1; // Type cast objects to Charts
			Chart c2 = (Chart) o2;
			switch (compType) {
			case BYNUMBER:
				return (c1.number < c2.number ? -1
						: (c1.number == c2.number ? 0 : 1));
			case BYNAME:
				return c1.name.compareTo(c2.name);
			case BYSCALE:
				return (c1.scale < c2.scale ? -1 : (c1.scale == c2.scale ? 0
						: 1));
			default:
				return 0; // Satisfy compiler; can't happen
			}
		}
	} // private inner class
} // Chart class


相关文章推荐

Map+List+ArrayList+LinkedList Java源码

  • 2010年07月01日 15:19
  • 696KB
  • 下载

java 集合(arraylist set map)遍历问题

1.遍历可以用for和iterator,其效率有区别 使用for循环与使用迭代器iterator的对比 效率上的各有有事 采用ArrayList对随机访问比较快,而for循环中的get()方法,...

Java集合框架List,Map,Set等全面介绍+vector VS ArrayList

比较java的几种框架/ 比较vector和ArrayList的性能和用法
  • easy41
  • easy41
  • 2016年10月19日 14:46
  • 160

Java 集合类 map set list arraylist hashmap hashtable

查看文章  Java 集合类 map set list arraylist hashmap hashtable2009-08-25 13:58Java 集合类 map set list arrayli...

java 容器集合类的区别用法(Vector ArrayList Map)

Set,List,Map,Vector,ArrayList的区别 JAVA的容器---List,Map,Set Collection ├List │├LinkedList │├ArrayLi...

Java集合删除元素ArrayList为例子分析

AbstractCollection集合类中有一个remove方法,该方法为了适配多种不同的集合,允许删除空的元素,看这部分代码的时候产生了疑问,为什么这里直接用it.remove()就直接删除了? ...

java中集合类HashSet、ArrayList、LinkedList总结以及例子

java中集合类HashSet、ArrayList、LinkedList总结、代码例子总结

《黑马程序员》java笔记->集合List中:ArrayList,LinkedList,及队列设计例子

------- android培训、java培训、期待与您交流! ---------- public class ArrayListTest { /** * @par...
  • bb175pt
  • bb175pt
  • 2013年06月16日 18:05
  • 536

[bxd学习java基本点]9.ArrayList存对象例子,其除重复及删除,要使用地址,而不是eqauls

day14-11 List集合看元素是否相同,依据是元素的方法 比较的是对象, ArrayList 存对象,就是把一人,包含姓名,年龄 每调一次contains,contains就要调equ...
  • bonlog
  • bonlog
  • 2013年04月13日 12:21
  • 1692
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:用于学习 Java 集合的 小例子(ArrayList,map)
举报原因:
原因补充:

(最多只允许输入30个字)