Java ListResourceBundle getKeys() 方法



描述

Java ListResourceBundle getKeys() 方法返回此 ResourceBundle 及其父捆綁包中包含的鍵的列舉。

宣告

以下是 java.util.ListResourceBundle.getKeys() 方法的宣告

public Enumeration<String> getKeys()

引數

返回值

此方法返回此 ResourceBundle 及其父捆綁包中包含的鍵的列舉。

異常

獲取字串、整數對 ListResourceBundle 的鍵的示例

以下示例演示了 Java ListResourceBundle getKeys() 方法的使用,用於迭代 ListResourceBundle 的鍵。我們建立了一個 ListResourceBundle 物件,其內容是字串、整數對的二維陣列。然後透過重寫 getContents() 方法添加了一些條目,然後檢索並迭代鍵的列舉以列印鍵。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.ListResourceBundle;

// create a class that extends to ListResourceBundle
class MyResources extends ListResourceBundle {

   // get contents must be implemented
   protected Object[][] getContents() {
      return new Object[][] {
         {"1", 1},
         {"2", 2},
         {"3", 3}
      };
   }
}

public class ListResourceBundleDemo {
   public static void main(String[] args) {

      // create a new MyResources instance
      MyResources mr = new MyResources();

      // get the keys
      Enumeration<String> enumeration = mr.getKeys();

      // print the keys
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement()); 
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

1
2
3

獲取字串、字串對 ListResourceBundle 的鍵的示例

以下示例演示了 Java ListResourceBundle getKeys() 方法的使用,用於迭代 ListResourceBundle 的鍵。我們建立了一個 ListResourceBundle 物件,其內容是字串、字串對的二維陣列。然後透過重寫 getContents() 方法添加了一些條目,然後檢索並迭代鍵的列舉以列印鍵。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.ListResourceBundle;

// create a class that extends to ListResourceBundle
class MyResources extends ListResourceBundle {

   // get contents must be implemented
   protected Object[][] getContents() {
      return new Object[][] {
         {"1", "Hello World!"},
         {"2", "Goodbye World!"},
         {"3", "Goodnight World!"}
      };
   }
}

public class ListResourceBundleDemo {
   public static void main(String[] args) {

      // create a new MyResources instance
      MyResources mr = new MyResources();

      // get the keys
      Enumeration<String> enumeration = mr.getKeys();

      // print the keys
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement()); 
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

1
2
3

獲取字串、物件對 ListResourceBundle 的鍵的示例

以下示例演示了 Java ListResourceBundle getKeys() 方法的使用,用於迭代 ListResourceBundle 的鍵。我們建立了一個 ListResourceBundle 物件,其內容是字串、Student 物件對的二維陣列。然後透過重寫 getContents() 方法添加了一些條目,然後檢索並迭代鍵的列舉以列印鍵。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.ListResourceBundle;

// create a class that extends to ListResourceBundle
class MyResources extends ListResourceBundle {

   // get contents must be implemented
   protected Object[][] getContents() {
      return new Object[][] {
         {"1", new Student(1, "Julie")},
         {"2", new Student(2, "Robert")},
         {"3", new Student(3, "Adam")}
      };
   }
}

public class ListResourceBundleDemo {
   public static void main(String[] args) {

      // create a new MyResources instance
      MyResources mr = new MyResources();

      // get the keys
      Enumeration<String> enumeration = mr.getKeys();

      // print the keys
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement()); 
      }
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

[ 1, Julie ]
[ 2, Robert ]
[ 3, Adam ]
java_util_listresourcebundle.htm
廣告
© . All rights reserved.