8_Java_对象容器

顺序容器

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

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

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

记事本
  • 能存储记录
  • 不限制能存储的记录的数量
  • 能知道以及存储的记录的数量
  • 能查看存进去的每一条记录
  • 能删除一条记录
  • 能列出所有的记录
接口设计
  • add(String note);
  • getSize();
  • getNote(int index);
  • removeNote(int index);
  • list();
范型类 ArrayList<>
	private ArrayList<String> notes = new ArrayList<String>();
	// ArrayList<类型> 名称 = new ArrayList<类型>();

ArrayList<> 本身是一个类,而 notes 是对象管理者;容器类用来存放对象;
容器类有两个类型:

  • 容器的类型;即这里的ArrayList
  • 元素的类型;即这里的String
添加

添加 notes.add() 添加进去的时候是有顺序的 先添加进去的在前面;
获取大小 notes.getsizre()

package notebook;

import java.util.ArrayList;

public class NoteBook {
	private ArrayList<String> notes = new ArrayList<String>();
	public void add(String note)
	{
		notes.add(note);
	}
	public String getNote(int index)
	{
		return "";
	}
	public int getSize()
	{
		return notes.size();
	}
	public boolean removeNote(int index)
	{
		return true;
	}
	public String[] list()
	{
		return new String[10];
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NoteBook nb = new NoteBook();
		nb.add("first");
		nb.add("second");
		System.out.println(nb.getSize());// 2
	}

}

添加到指定下标的前面 notes.add(String note, int location)

package notebook;

import java.util.ArrayList;

public class NoteBook {
	private ArrayList<String> notes = new ArrayList<String>();
	public void add(String note)
	{
		notes.add(note);
	}
	public void add(String note, int location)
	{
		notes.add(location, note);
	}
	public String getNote(int index)
	{
		return notes.get(index);
	}
	public int getSize()
	{
		return notes.size();
	}
	public boolean removeNote(int index)
	{
		return true;
	}
	public String[] list()
	{
		return new String[10];
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NoteBook nb = new NoteBook();
		nb.add("first");
		nb.add("second");
		nb.add("thrild", 1);
		System.out.println(nb.getSize());// 2
		System.out.println(nb.getNote(1));
	}

}

删除 notes.remove(index)
列出所有 notes.list()

package notebook;

import java.util.ArrayList;

public class NoteBook {
	private ArrayList<String> notes = new ArrayList<String>();
	public void add(String note)
	{
		notes.add(note);
	}
	public void add(String note, int location)
	{
		notes.add(location, note);
	}
	public String getNote(int index)
	{
		return notes.get(index);
	}
	public int getSize()
	{
		return notes.size();
	}
	public void removeNote(int index)
	{
		notes.remove(index);
	}
	public String[] list()
	{
		String[] a = new String[notes.size()];
		notes.toArray(a); // 帮我们把数组填起来
//		for(int i = 0; i < notes.size(); i ++)
//		{
//			a[i] = notes.get(i);
//		}
		return a;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NoteBook nb = new NoteBook();
		nb.add("first");
		nb.add("second");
		nb.add("thrild", 1);
		System.out.println(nb.getSize());// 2
		System.out.println(nb.getNote(1));
		nb.removeNote(2);
		System.out.println(nb.getSize());
		String[] b = nb.list();
		for(String s : b)
		{
			System.out.println(s);
		}
	}

}
对象数组

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

String[] a = new String[10];
System.out.println(a[0].length());

这个代码会报错,原因在于这个数组存放的是对象的管理者而不是对象本身;

package third;

public class Third {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = new int[3];
		for(int i = 0; i < a.length; i ++)
		{
			a[i] = i;
		}
		for(int v : a)
		{
			System.out.println(v);
			v = 0;
		}
		for(int v : a)
		{
			System.out.println(v);
		}
	}
}

上面可以看到,用 for - each 是改不掉的;

package third;

class Value{
	private int i;
	public void set(int i)
	{
		this.i = i;
	}
	public int get()
	{
		return i;
	}
}
public class Third {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Value[] a = new Value[3];
		for(int i = 0; i < a.length; i ++)
		{
			a[i] = new Value();
			a[i].set(i);
		}
		for(Value v : a)
		{
			System.out.println(v.get());
			v.set(0);
		}
		for(Value v : a)
		{
			System.out.println(v.get());
		}
	}

}

而对象数组是可以改掉的,因为是多个管理者对应一个对象本身,通过管理者就可以找到本身;而单纯的数组,只是改的复制过来的值,本身并没有什么改变;

ArrayList<String> a = new ArrayList<String>();
// 也是可以使用 for - each 的
函数传递任意的参数

函数传递任意的参数,以及规定的最少的参数;

package notebook;

import java.util.ArrayList;

public class NoteBook {
	public static void fun(int num, int ...values)
	{
		//输出一个
		System.out.println(num);
		//输出任意个
		for(int s : values)
		{
			System.out.println(s);
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		fun(1, 2, 3);
	}

}
Varargs
集合容器

集合就是数学中的集合的概念:所有的元素都具有唯一的值,元素在其中没有顺序。
所谓的唯一是指输入的重复的当一个来处理;

ArrayList 重复的元素不会处理成一个

package notebook;

import java.util.ArrayList;

public class NoteBook {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<String> a = new ArrayList<String>();
		a.add("1");
		a.add("2");
		a.add("2");
		for(String s : a)
		{
			System.out.println(s);
		}
	}

}
/*
1
2
2
*/

HashSet 元素无序,重复的处理成一个

package notebook;

import java.util.HashSet;

public class NoteBook {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashSet<String> a = new HashSet<String>();
		a.add("1");
		a.add("2");
		a.add("1");
		for(String s : a)
		{
			System.out.println(s);
		}
	}

}
/*
1
2
*/
package notebook;

import java.util.ArrayList;
import java.util.HashSet;

public class NoteBook {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashSet<String> a = new HashSet<String>();
		a.add("2");
		a.add("1");
		a.add("1");
		System.out.println(a);
		ArrayList<String> b = new ArrayList<String>();
		b.add("2");
		b.add("1");
		b.add("1");
		System.out.println(b);
	}

}
/*
[1, 2]
[2, 1, 1]
*/
直接输出对象 toString 函数
package notebook;

import java.util.ArrayList;
import java.util.HashSet;

class Value{
	private int i;
	public void set(int i)
	{
		this.i = i;
	}
	public int get()
	{
		return i;
	}
	public String toString() {
		return "" + i;
	}
}
public class NoteBook {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Value v = new Value();
		v.set(10);
		System.out.println(v);
	}

}
// 10
散列表 Hash
private HashMap<Integer, String> a = new HashMap<Integer, String>();

同一个键 key 总是取最后一个键的元素;

package coins;

import java.util.HashMap;

public class Coin {
	private HashMap<Integer, String> a = new HashMap<Integer, String>();
	public Coin()
	{
		a.put(1, "DL1");
		a.put(2, "DL2");
		a.put(3, "DL3");
		a.put(3, "DL4");//重复的取最后一个
		System.out.println(a.keySet().size());
		System.out.println(a.keySet());
		System.out.println(a);
		for(Integer k : a.keySet())
		{
			String s = a.get(k);
			System.out.println(s);
		}
	}
	public String getName(int amount)
	{
		if(a.containsKey(amount))
		{
			return a.get(amount);
		}
		else
		{
			return "Not Found!!!";
		}
	}
	public String toString()
	{
		return a + "";
	}
	public static void main(String[] args) {
		Coin coin = new Coin();
		System.out.println(coin.getName(0));
		System.out.println(coin.getName(1));
		System.out.println(coin); //需要 toString() 函数
	}

}
/*
3
[1, 2, 3]
{1=DL1, 2=DL2, 3=DL4}
DL1
DL2
DL4
Not Found!!!
DL1
{1=DL1, 2=DL2, 3=DL4}
*/

package coins;

import java.util.HashMap;

public class Coin {
	private HashMap<String, HashMap<String, Integer>> a = new HashMap<String, HashMap<String, Integer>>();
	public Coin()
	{
		
	}
	public void set(String name, String name_b, int score)
	{
		if(a.containsKey(name))
		{
			a.get(name).put(name_b, score);
		}
		else
		{
			HashMap<String, Integer> b = new HashMap<String, Integer>();
			b.put(name_b, score);
			a.put(name, b);
		}
	}
	public HashMap<String, Integer> getName(String name)
	{
		return a.get(name);
	}
	public HashMap<String, Integer> getSore(String name)
	{
		HashMap<String, Integer> m = new HashMap<>();
		for(String v : a.keySet())
		{
			if(a.get(v).containsKey(name))
			{
				m.put(v, a.get(v).get(name));
			}
		}
		return m;
	}
	public String toString()
	{
		return a + "";
	}
	public static void main(String[] args) {
		Coin coin = new Coin();
		coin.set("DL", "语文", 99);
		coin.set("DL", "数学", 90);
		coin.set("CLL", "语文", 100);
		coin.set("CLL", "数学", 99);
		System.out.println(coin.getName("DL"));
		System.out.println(coin.getSore("数学"));
		System.out.println(coin);
	}

}
/*
{数学=90, 语文=99}
{CLL=99, DL=90}
{CLL={数学=99, 语文=100}, DL={数学=90, 语文=99}}
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
JList 是 Java Swing 组件中的一个列表组件,可以用来展示一组数据。以下是 JList 的用法: 1. 创建 JList 对象: ``` JList list = new JList(); ``` 2. 设置数据模型: JList 组件需要一个数据模型来存储数据,可以使用 DefaultListModel 类来创建数据模型: ``` DefaultListModel model = new DefaultListModel(); model.addElement("Item 1"); model.addElement("Item 2"); list.setModel(model); ``` 3. 设置显示样式: 可以通过设置 ListCellRenderer 对象来改变每个列表项的显示样式: ``` list.setCellRenderer(new MyListRenderer()); ``` 4. 添加到容器中: 将 JList 添加到容器中即可显示出来: ``` JScrollPane scrollPane = new JScrollPane(list); frame.getContentPane().add(scrollPane); ``` 完整示例代码: ``` import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.ListCellRenderer; public class MyListRenderer implements ListCellRenderer<String> { public static void main(String[] args) { JFrame frame = new JFrame(); JList<String> list = new JList<String>(); DefaultListModel<String> model = new DefaultListModel<String>(); model.addElement("Item 1"); model.addElement("Item 2"); list.setModel(model); list.setCellRenderer(new MyListRenderer()); JScrollPane scrollPane = new JScrollPane(list); frame.getContentPane().add(scrollPane); frame.pack(); frame.setVisible(true); } public MyListRenderer() { } @Override public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) { JLabel label = new JLabel(); label.setText(value); if (isSelected) { label.setBackground(list.getSelectionBackground()); label.setForeground(list.getSelectionForeground()); } else { label.setBackground(list.getBackground()); label.setForeground(list.getForeground()); } label.setOpaque(true); return label; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清纯献给了作业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值