Java面向对象程序设计第三章

容器是现代程序设计非常基础而重要的手段。

所谓容器,就是“放东西的东西”。数组可以看作是一种容器,但是数组的元素个数一旦确定就无法改变,这在实际使用中是很大的不足。一般意义上的容器,是指具有自动增长容量能力的存放数据的一种数据结构。在面向对象语言中,这种数据结构本身表达为一个对象。所以才有“放东西的东西”的说法。

Java具有丰富的容器,Java的容器具有丰富的功能和良好的性能。熟悉并能充分有效地利用好容器,是现代程序设计的基本能力。

我们首先学习的是顺序容器,即放进容器中的对象是按照指定的顺序(放的顺序)排列起来的,而且允许具有相同值的多个对象存在。

在一些书中,将容器(英文为collection或container)翻译为“集合”,由于数学中的集合(Set)也是一种特定的容器类型,我们认为将collection翻译为集合是不恰当的。所以我们只会使用容器一词。

Array_list

import java.util.*;

public class Notebook {
	private ArrayList<String> notes = new ArrayList<String>();//容器类型<元素类型>

	
	public void add(String s)
	{
		notes.add(s);
	}
	
	public int getSize()
	{
		return notes.size();
	}
	
	public void add(String s, int location)
	{
		notes.add(location,s);
	}
	public String getNote(int index)
	{
		return notes.get(index);
	}
	
	public void removeNote(int index)
	{
		notes.remove(index);
	}
	
	public String[] list ()
	{
		String[] a = new String[notes.size()];
//		for( int i =0; i<notes.size();i++ )
//		{
//			a[i] = notes.get(i);
//		}
		notes.toArray(a);
		return a;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] a = new String[2];
		a[0] = "first";
		a[1] = "second";
		Notebook nb = new Notebook();
		nb.add("first");
		nb.add("second");
		nb.add("third", 1);
		System.out.println(nb.getSize());
		System.out.println(nb.getNote(1));	
		nb.removeNote(1);
		String[] b = nb.list();
		for ( String s : a)
		{
			System.out.println(s);
		}

	}

}

对象数组:当数组的元素的类型是类的时候,数组的每一个元素其实只是对象的管理者而不是对象本身。因此,仅仅创建数组并没有创建其中的每一个对象!

import java.util.*;

public class Notebook {
	private ArrayList<String> notes = new ArrayList<String>();//容器类型<元素类型>

	
	public void add(String s)
	{
		notes.add(s);
	}
	
	public int getSize()
	{
		return notes.size();
	}
	
	public void add(String s, int location)
	{
		notes.add(location,s);
	}
	public String getNote(int index)
	{
		return notes.get(index);
	}
	
	public void removeNote(int index)
	{
		notes.remove(index);
	}
	
	public String[] list ()
	{
		String[] a = new String[notes.size()];
//		for( int i =0; i<notes.size();i++ )
//		{
//			a[i] = notes.get(i);
//		}
		notes.toArray(a);
		return a;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<String> a = new ArrayList<String>();
		a.add("first");
		a.add("second");
		for( String s : a)
		{
			System.out.println(s);
		}
	}

}

集合就是数学中的集合的概念:所有的元素都具有唯一的值,元素在其中没有顺序。

import java.util.*;

class Value{
	private int i;
	public void set(int i) {this.i = i;}
	public String toString() {return "" + i;}//把i变成字符串
}

public class Notebook {
	private ArrayList<String> notes = new ArrayList<String>();//容器类型<元素类型>

	
	public void add(String s)
	{
		notes.add(s);
	}
	
	public int getSize()
	{
		return notes.size();
	}
	
	public void add(String s, int location)
	{
		notes.add(location,s);
	}
	public String getNote(int index)
	{
		return notes.get(index);
	}
	
	public void removeNote(int index)
	{
		notes.remove(index);
	}
	
	public String[] list ()
	{
		String[] a = new String[notes.size()];
//		for( int i =0; i<notes.size();i++ )
//		{
//			a[i] = notes.get(i);
//		}
		notes.toArray(a);
		return a;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Value v = new Value();
		v.set(10);
		System.out.println(v);
		System.out.println("--------------");
		ArrayList<String> a = new ArrayList<String>();
		a.add("first");
		a.add("second");
		a.add("first");
		System.out.println(a);//主动调用 toString函数
//		for( String s : a)
//		{
//			System.out.println(s);
//		}
		System.out.println("--------------");
		HashSet<String> s = new HashSet<String>(); //集合
		s.add("first");
		s.add("second");
		s.add("first");
		System.out.println(s);//主动调用 toString函数
//		for( String k : s)
//		{
//			System.out.println(k);
//		}
	}

}

传统意义上的Hash表,是能以int做值,将数据存放起来的数据结构。Java的Hash表可以以任何实现了hash()函数的类的对象做值来存放对象。

Hash表是非常有用的数据结构,熟悉它,充分使用它,往往能起到事半功倍的效果。

import java.util.*;

public class Coin {
	private HashMap<Integer, String> coinnames = new HashMap<Integer, String>();//Hash表中所有的元素都是对象 Hash表中key键唯一
//无论键值是否相同 只留下最后一次的key对应的键值
	public Coin()
	{
		coinnames.put(1, "penny");//1的位置上存储penny
		coinnames.put(10, "dime");
		coinnames.put(25, "quarter");
		coinnames.put(50, "half-quarter");
		coinnames.put(50, "五毛");//覆盖half-quarter
		System.out.println(coinnames.keySet().size());
		System.out.println(coinnames);
		for ( Integer k : coinnames.keySet() )
		{
			String s = coinnames.get(k);
			System.out.println(s);
		}
	}
	
	public String getName( int amount )
	{
		if( coinnames.containsKey(amount) )
		{
			return coinnames.get(amount);
		}
		else
		{
		return "Not Found";
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int amount = in.nextInt();
		Coin coin = new Coin();
		System.out.println("-------");
		String name = coin.getName(amount);
		System.out.println(name);
	}

}

1

查找里程(10分)

题目内容:

下图为国内主要城市之间的公路里程:

 

 

你的程序要读入这样的一张表,然后,根据输入的两个城市的名称,给出这两个城市之间的里程。

 

注意:任何两个城市之间的里程都已经给出,不需要计算经第三地中转。

注意:你并不需要去录入上图的数据,数据是在程序输入中给的。

 

输入格式:

首先,你会读到若干个城市的名字。每个名字都只是一个英文单词,中间不含空格或其他符号。当读到名字为“###”(三个#号)时,表示城市名字输入结束,###并不是一个城市的名字。如果记读到的城市名字的数量为n。

然后,你会读到nxn的一个整数矩阵。第一行的每一个数字,表示上述城市名单中第一个城市依次到另一个城市之间的里程。表中同一个城市之间的里程为0。

最后,你会读到两个城市的名字。

 

输出格式:

输出这两个城市之间的距离。

 

输入样例:

Hagzou Hugzou Jigxng    ###

0 1108 708

1108 0 994

708 994 0

Hagzou    Jigxng

 

输出样例:

708

 

时间限制:500ms内存限制:32000kb

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Distance {
	private ArrayList<String> cities = new ArrayList<String>();
	private HashMap<String, HashMap<String, Integer>> distance = new HashMap<String, HashMap<String, Integer>>();//嵌套哈希映射
	private static Scanner in = new Scanner(System.in);
	private String start, end;//记录始发站站名和终到站站名
	
	public void input()
	{
		String city;
		//读入城市名
		while(true)
		{
			city = in.next();
			if( city.equals("###") == true )
				break;
			else
			{
				cities.add(city);
			}
		}
		//读入城市间距离
		for( int i = 0; i<cities.size(); i++)
		{
			HashMap<String, Integer> city_hashmap = new HashMap<String, Integer>();
			for( int j = 0; j<cities.size(); j++ )
			{
				int dis = in.nextInt();
				city_hashmap.put(cities.get(j),dis);//the key:cities.get(j) ----> value of the key: dis
			}
			distance.put(cities.get(i), city_hashmap);
		}
	}
	
	public void CitytoCity_distance()
	{
		int dis = 0;
		this.start = in.next();
		this.end = in.next();
		dis = distance.get(start).get(end);//HashMap的嵌套
		System.out.println(dis);
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Distance dis = new Distance();
		dis.input();
		dis.CitytoCity_distance();
		in.close();

	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值