📜  Java中的属性 elements() 方法和示例

📅  最后修改于: 2022-05-13 01:54:23.590000             🧑  作者: Mango

Java中的属性 elements() 方法和示例

Properties 类elements()方法用于获取此 Properties 对象的枚举。它可以进一步用于使用收到的此枚举按顺序检索元素。

句法:

public Enumeration elements()

参数:此方法不接受任何参数。
返回:此方法返回此 Properties 对象中的值的枚举
下面的程序说明了 elements() 方法:
方案一:

Java
// Java program to demonstrate
// elements() method.
 
import java.util.*;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
    {
 
        // Create a properties and add some values
        Properties properties = new Properties();
        properties.put("Pen", 10);
        properties.put("Book", 500);
        properties.put("Clothes", 400);
        properties.put("Mobile", 5000);
 
        // Print Properties details
        System.out.println("Current Properties: "
                           + properties.toString());
 
        // Creating an empty enumeration to store
        Enumeration enu = properties.elements();
 
        System.out.println("The enumeration of values are:");
 
        // Displaying the Enumeration
        while (enu.hasMoreElements()) {
            System.out.println(enu.nextElement());
        }
    }
}


Java
// Java program to demonstrate
// elements() method.
 
import java.util.*;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
    {
 
        // Create a properties and add some values
        Properties properties = new Properties();
 
        properties.put(1, "100RS");
        properties.put(2, "500RS");
        properties.put(3, "1000RS");
 
        // print Properties details
        System.out.println("Current Properties: "
                           + properties.toString());
 
        // Creating an empty enumeration to store
        Enumeration enu = properties.elements();
 
        System.out.println("The enumeration of values are:");
 
        // Displaying the Enumeration
        while (enu.hasMoreElements()) {
            System.out.println(enu.nextElement());
        }
    }
}


输出:
Current Properties: {Book=500, Mobile=5000, Pen=10, Clothes=400}
The enumeration of values are:
500
5000
10
400

方案二:

Java

// Java program to demonstrate
// elements() method.
 
import java.util.*;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
    {
 
        // Create a properties and add some values
        Properties properties = new Properties();
 
        properties.put(1, "100RS");
        properties.put(2, "500RS");
        properties.put(3, "1000RS");
 
        // print Properties details
        System.out.println("Current Properties: "
                           + properties.toString());
 
        // Creating an empty enumeration to store
        Enumeration enu = properties.elements();
 
        System.out.println("The enumeration of values are:");
 
        // Displaying the Enumeration
        while (enu.hasMoreElements()) {
            System.out.println(enu.nextElement());
        }
    }
}
输出:
Current Properties: {3=1000RS, 2=500RS, 1=100RS}
The enumeration of values are:
1000RS
500RS
100RS

参考资料:https://docs.oracle.com/javase/9/docs/api/ Java/util/Properties.html#elements–