(Java)其他集合类

一、Stack 类

栈是采用先进后出的数据存储方式,每一个栈都包含一个栈顶,每次出栈是将栈顶的数据取出:
在这里插入图片描述
在浏览器中存在一个后退按钮,每次后退都是后退到上一步的操作,这就是一个栈的应用,采用的是先进后出的操作

Java 中使用 Stack 类进行栈的操作,Stack 类是 Vector 的子类,Stack 类的定义:

public class Stack<E> extends Vector<E>

Stack 类的常用方法:

在这里插入图片描述

import java.util.Stack;

public class Test{
    public static void main(String[] args) {
        Stack<String> s = new Stack<String >();//实例化 Stack 对象
        s.push("A");
        s.push("B");
        s.push("C");
        System.out.print(s.pop() + "、");//出栈
        System.out.print(s.pop() + "、");
        System.out.print(s.pop() + "、");
        System.out.print(s.pop() + "、");//错误,栈为空时出栈异常
    }
}

在这里插入图片描述

二、属性类:Properties

1. Properties 类简介

在一个属性文件中保存了多个属性,每一个属性就是直接用字符串表示出来的“key=value对”,而如果要轻松地操作这些类属性文件中的属性,可以通过 Properties 类方便地完成。

通过 “key=value” 形式保存的文件就是属性文件

Properties 类的定义:

public class Properties extends Hashtable<Object,Object>

Properties 类的主要方法:
在这里插入图片描述

XML(eXtensible Markup Language,可扩展的标记性语言)是在现在开发中使用最广泛的一门语言,所有的属性可以通过 storeToXML() 和 loadFormXML() 两个方法以 XML we年格式进行保存和读取,但是使用 XML 格式保存时将按照指定的文档格式进行存放,如果格式出错,将无法读取

2. Properties 操作实例

(1)实例操作一:设置和取得属性
可以使用 setProperty() 和 getProperty() 方法设置和取得属性,操作时要以 String 为操作类型。

import java.util.Properties;

public class Root{
    public static void main(String[] args) {
        Properties pro = new Properties();//创建 Properties 对象
        pro.setProperty("1","J1");
        pro.setProperty("2","J2");
        pro.setProperty("3","J3");
        System.out.println("1 属性存在:" + pro.getProperty("1"));
        System.out.println("2 属性存在:" + pro.getProperty("2"));
        System.out.println("4 属性不存在,同时设置显示的默认值:" + pro.getProperty("4","没有发现"));
    }
}

在这里插入图片描述
(2)实例操作二:将属性保存在普通文件中
正常属性类操作完成后,可以将其内容保存在文件中,可以直接使用 sote() 方法即可,同时指定 OutputStream 类型,指明输出的位置。属性文件的后缀是任意的,但是最好按照标准,将属性文件的后缀统一设置成 “.properties

public class Test{
    public static void main(String[] args) {
        Properties pro = new Properties();
        pro.setProperty("A","Java");
        pro.setProperty("B","Python");
        pro.setProperty("C","C++");
//        设置属性文件的保存路径
        File file = new File("D:" + File.separator + "area.properties");
        try{//保存属性到普通文件中,并设置注释内容
            pro.store(new FileOutputStream(file),"Area Info");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

在这里插入图片描述
(3)实例操作三:从普通文件中读取属性内容
既然可以保存,就可以通过 load() 方法,从输入流中将所保存的所有属性内容读取出来。

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class Test{
    public static void main(String[] args) {
        Properties pro = new Properties();
//        设置属性文件的保存路径
        File file = new File("D:" + File.separator + "area.properties");
        try{//保存属性到普通文件中,并设置注释内容
            pro.load(new FileInputStream(file));//读取属性文件
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("B 属性值存在,内容是:" + pro.getProperty("2"));
    }
}

在这里插入图片描述
(4)实例操作四:将属性保存在 XML 文件中
在 Properties 类中也可以把全部内容以 XML 格式的内容通过输出流输出,如果要把属性保存在 XML 文件中,则文件的后缀最好为 “*.xml”

public class Test{
    public static void main(String[] args) {
        Properties pro = new Properties();
        pro.setProperty("A","Java");
        pro.setProperty("B","Python");
        pro.setProperty("C","C++");
//        设置属性文件的保存路径
        File file = new File("D:" + File.separator + "area.xml");
        try{//保存属性到普通文件中,并设置注释内容
            pro.storeToXML(new FileOutputStream(file),"Area Info");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

(5)从 XML 文件中读取属性
以 XML 文件格式输出全部属性后,必须使用 loadFromXML() 方法将内容读取进来。

public class Test{
    public static void main(String[] args) {
        Properties pro = new Properties();
//        设置属性文件的保存路径
        File file = new File("D:" + File.separator + "area.xml");
        try{
            pro.loadFromXML(new FileInputStream(file));
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("B 属性存在,内容为:" + pro.getProperty("B"))
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

南淮北安

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

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

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

打赏作者

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

抵扣说明:

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

余额充值