Java基础语法

public class Hello
{   
    public static void main(String[] args)
    {
    
    }
}
Array
int[] anArray=new int[10];
anArray[0] = 100;
char[] array = {'d', 'e', 'c', 'a', 'f', 'f', 'e',
            'i', 'n', 'a', 't', 'e', 'd'};
Arrays.sort(array);

List
List<String> list = new ArrayList<String>();
list.add( "work" );
String strValue=list.get(0);
if(list.contains("work")System.out.print("ok");
list.set( 1, "walk" )

Map

Map<String, String> map = new HashMap<String, String>();
map.put( "lemon", "黄" );
System.out.println( map.get( "lemon" ) );
for ( String key : map.keySet() ) {
	String data = map.get( key );
	
	System.out.println( key + ": " + data );
}
      HashMap newmap = new HashMap();
      
      // populate hash map
      newmap.put(1, "tutorials");
 System.out.println("Check if key 2 exists: " + newmap.containsKey(2));


Iterator entries = myMap.entrySet().iterator();
while (entries.hasNext()) {
  Entry thisEntry = (Entry) entries.next();
  Object key = thisEntry.getKey();
  Object value = thisEntry.getValue();
  // ...
}



Set

 HashSet<String> hs = new HashSet<String>();
hs.add("Z");

Iterator<String> it = hs.iterator();
while (it.hasNext()) {
   System.out.println(it.next());
}
if(hs.contains("Z"))<span style="font-family: Arial, Helvetica, sans-serif;">System.out.println("ok");</span>

Hashtable

Hashtable<String, Integer> numbers
     = new Hashtable<String, Integer>();
   numbers.put("one", 1);
   numbers.put("two", 2);
   numbers.put("three", 3);
 Integer n = numbers.get("two");
   if (n != null) {
     System.out.println("two = " + n);
   }
Set<String> setKeys=numbers.keySet();

Implement a hashtable

public class HashEntry {
      private int key;
      private int value;
 
      HashEntry(int key, int value) {
            this.key = key;
            this.value = value;
      }     
 
      public int getKey() {
            return key;
      }
 
      public int getValue() {
            return value;
      }
}

public class HashMap {
      private final static int TABLE_SIZE = 128;
 
      HashEntry[] table;
 
      HashMap() {
            table = new HashEntry[TABLE_SIZE];
            for (int i = 0; i < TABLE_SIZE; i++)
                  table[i] = null;
      }
 
      public int get(int key) {
            int hash = (key % TABLE_SIZE);
            while (table[hash] != null && table[hash].getKey() != key)
                  hash = (hash + 1) % TABLE_SIZE;
            if (table[hash] == null)
                  return -1;
            else
                  return table[hash].getValue();
      }
 
      public void put(int key, int value) {
            int hash = (key % TABLE_SIZE);
            while (table[hash] != null && table[hash].getKey() != key)
                  hash = (hash + 1) % TABLE_SIZE;
            table[hash] = new HashEntry(key, value);
      }
}

For string keys, we'll use the following method to hash.

public int hash(String key) {
    int hashkey = key.hashCode();
    return hashkey % arraySize;
} // END hash()




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值