⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample14_15.java

📁 Java SE 6.0前12-16章示的示例代码,简单易学
💻 JAVA
字号:
package wyf.jc;
import java.util.*;
//元素类
class MyEntryForEach
{
	//元素的成员变量声明
	int intMember;
	//构造器
	public MyEntryForEach(int intMember)
	{
		this.intMember=intMember;
	}
	//重写toString方法
	public String toString()
	{
		return this.intMember+"";
	}
}
//主类
public class Sample14_15
{
	public static void main(String[] args)
	{
	     //创建了HashSet对象
	     HashSet hs=new HashSet();
	     //向HashSet对象中添加内容不同的学生对象元素
	     hs.add(new MyEntryForEach(23));
	     hs.add(new MyEntryForEach(24));
	     hs.add(new MyEntryForEach(25));
	     hs.add(new MyEntryForEach(27));
	     hs.add(new MyEntryForEach(29));
	     //打印输出HashSet中的内容
		 System.out.print("这里是HashSet操作前的结果:");
	     System.out.println(hs);
	     //通过for-each循环得到元素并修改其中的元素
	     for(Object o:hs)
	     {
	          //对元素引用进行强制类型转换
	          MyEntryForEach temp=(MyEntryForEach)o;
	          temp.intMember=55;
	     }
	     //打印输出HashSet中的内容
		 System.out.print("这里是HashSet操作后的结果:");
	     System.out.println(hs);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -