楼主: Lisrelchen
823 2

[Java Example]How to use Dictionary in Java [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4192份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50278 个
通用积分
83.5106
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2016-9-1 00:20:32 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. /*
  2. Dictionary implements a key value pair kind of collection
  3. where the key is unique
  4. */
  5. import java.util.*;
  6. public class DictionaryDemo {
  7.   static String newLine = System.getProperty("line.separator");
  8.   public static void main(String[] args) {
  9.   
  10.     System.out.println(newLine + "Dictionary in Java" + newLine);
  11.     System.out.println("-----------------------" + newLine);
  12.     System.out.println("Adding items to the Dictionary" + newLine);
  13.     //Creating dictionary object
  14.     //dictionary can be created using HashTable object
  15.     //as dictionary is an abstract class
  16.     Dictionary dict = new Hashtable();
  17.    
  18.     //you add elements to dictionary using put method
  19.     //put(key, value)
  20.     dict.put(1, "Java");
  21.     dict.put(2, ".NET");
  22.     dict.put(3, "Javascript");
  23.     dict.put(4, "HTML");
  24.    
  25.     System.out.println(newLine + "Items in the dictionary..." + dict + newLine);
  26.     System.out.println("-----------------------" + newLine);
  27.    
  28.     //elements can be retrieved using their key
  29.     System.out.println("Retrieve element from dictionary with key 1 : " +
  30.     dict.get(1) + newLine);
  31.     System.out.println("-----------------------" + newLine);
  32.    
  33.     //elements can be removed using their key
  34.     System.out.println("Removing element from dictionary with key 2 : " +
  35.     dict.remove(2) + newLine);
  36.    
  37.     System.out.println("Items in the dictionary after removing..." + dict + newLine);
  38.     System.out.println("-----------------------" + newLine);
  39.   }
  40. }
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Dictionary example ExamP Java AMPL dictionary created public import Java

沙发
Lisrelchen 发表于 2016-9-1 00:21:41
Output
  1. Executing Run Command: java -classpath /root DictionaryDemo                                                                                                                                                                                                                                                           
  2.                                                                                                                                                                                                                                                                                                                      
  3.                                                                                                                                                                                                                                                                                                                      
  4. Dictionary in Java                                                                                                                                                                                                                                                                                                   
  5.                                                                                                                                                                                                                                                                                                                      
  6. -----------------------                                                                                                                                                                                                                                                                                               
  7.                                                                                                                                                                                                                                                                                                                      
  8. Adding items to the Dictionary                                                                                                                                                                                                                                                                                       
  9.                                                                                                                                                                                                                                                                                                                      
  10.                                                                                                                                                                                                                                                                                                                      
  11. Items in the dictionary...{4=HTML, 3=Javascript, 2=.NET, 1=Java}                                                                                                                                                                                                                                                      
  12.                                                                                                                                                                                                                                                                                                                      
  13. -----------------------                                                                                                                                                                                                                                                                                               
  14.                                                                                                                                                                                                                                                                                                                      
  15. Retrieve element from dictionary with key 1 : Java                                                                                                                                                                                                                                                                    
  16.                                                                                                                                                                                                                                                                                                                      
  17. -----------------------                                                                                                                                                                                                                                                                                               
  18.                                                                                                                                                                                                                                                                                                                      
  19. Removing element from dictionary with key 2 : .NET                                                                                                                                                                                                                                                                    
  20.                                                                                                                                                                                                                                                                                                                      
  21. Items in the dictionary after removing...{4=HTML, 3=Javascript, 1=Java}                                                                                                                                                                                                                                               
  22.                                                                                                                                                                                                                                                                                                                      
  23. -----------------------                                                                                                                                                                                                                                                                                               
  24.                                                                                                                                                                                                                                                                                                                      
  25.                                                                                                                                                                                                                                                                                                                      
  26. Process exited successfully      
复制代码

藤椅
Kamize 学生认证  发表于 2016-9-1 07:32:31 来自手机
Lisrelchen 发表于 2016-9-1 00:20
谢谢楼主

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-20 23:17