公告版位
需要找什麼嗎? 左邊分類或許可以幫助你...

    傳統 HashMap 必須輸入Key才能取得Value,但是常常會遇到沒有 Key 但是卻想要得到 Value ,這時可以考慮使用 HashMap 中的 keySet() 方法,要注意的是這種輸出結果的順序並不是依造放入(put)的順序,所以無法跟 ArrayList 一樣有順序性的,他只是把 HashMap 裡的 put 資料全部取出而已,這部分必須要注意。可以參考以下程式碼並比對輸出結果:

 

 import java.util.HashMap;

public class NewClass {

    public static void main(String[] args) {
        HashMap map = new HashMap();

        map.put("A", "111");
        map.put("B", "222");
        map.put("C", "333");
        map.put("D", "444");
        map.put("E", "555");
        map.put("F", "666");
        map.put("G", "777");
        map.put("H", "888");
        map.put("I", "999");


        for (Object key : map.keySet()) {
            System.out.println(key + " : " + map.get(key));
        }
    }
}


輸出結果如下:

AA11

 

 

 

 


黃彥霖 發表在 痞客邦 留言(1) 人氣()

    這邊主要的重點是 UdpClient 這個類別,雖然從字面上這個類別叫做 UDP 的 Client (客戶端),但實際上我們也可以用它來建立伺服器,簡單來說 UdpClient 類別可以同時建立 伺服器端 (Server) 與 客戶端 (Client) 兩種功能

    如果今天要建立伺服器端 UdpClient 則要宣告成 UdpClient uc = new UdpClient(5555); ,這裡的 5555 代表伺服器的 Port (監聽埠)。

    如果今天要建立客戶端 UdpClient 擇要宣告成 UdpClient uc = new UdpClient(); ,也就是不需要指定 Port,因為只是發送消息,並不是要接收,所以電腦會依流水號的方式分配Port,將資料傳到指定IP:Port上 (此時指定IP:Port是使用 IPEndPoint 類別)。

以下範例程式碼:

 

伺服器端:

 using System;
using System.Net;
using System.Net.Sockets;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("這是伺服器...\n");

        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5555);
        UdpClient uc = new UdpClient(ipep.Port);

        int i = 0;
        while (true)
        {
            Console.WriteLine(System.Text.Encoding.UTF8.GetString(uc.Receive(ref ipep)));
            byte[] b = System.Text.Encoding.UTF8.GetBytes("這是'伺服器'回傳的訊息 ~ " + i++);
            uc.Send(b, b.Length, ipep);
        }
    }
}

客戶端:

using System;
using System.Net;
using System.Net.Sockets;

class MainClass
{
        public static void Main (string[] args)
        {
                Console.WriteLine ("這是客戶端...\n");
                IPEndPoint ipep = new IPEndPoint (IPAddress.Parse ("127.0.0.1"), 5555);
                UdpClient uc = new UdpClient ();
            
                for (int i = 0; i<10; i++) {
                        byte[] b = System.Text.Encoding.UTF8.GetBytes ("這是'客戶端'傳送的訊息 ~ " + i);            
                        uc.Send (b, b.Length, ipep);
                        Console.WriteLine (System.Text.Encoding.UTF8.GetString (uc.Receive (ref ipep)));
                }                       
        }
}


執行結果:

aaa

黃彥霖 發表在 痞客邦 留言(0) 人氣()

先至 [ 管理後台 ] → [ 側邊欄位設定 ] → [ 頁尾描述 ] 貼上以下程式碼 :

<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shCore.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushCSharp.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushCss.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushDiff.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushPlain.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushSql.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushVb.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushXml.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/3.0.83/scripts/shBrushJava.js"></script>
<script type="text/javascript">
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/3.0.83/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/3.0.83/styles/shCore.css"/>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/3.0.83/styles/shThemeDefault.css" id="shTheme"/>

這樣就完成了。接著發布文章(程式碼)前,請先把你的程式碼先經以下網址轉碼,程式碼排版才不會亂掉。

 

HTML 部落格轉碼 

http://www.string-functions.com/htmlencode.aspx

接著把轉碼後的程式碼貼製 pre 標籤裡面,如下:

<pre class="brush: html">

</pre>

如果你寫的是HTML則請把 brush 換成 html 如:brush: html

如果你寫的是JavaScript則請把 brush 換成 js如:brush: js

如果你寫的是C#則請把 brush 換成 js如:brush: csharp

 

黃彥霖 發表在 痞客邦 留言(1) 人氣()

using UnityEngine;
using System.Collections;
using System.Threading;
 
public class Test : MonoBehaviour
{
        My m;
 
        void Start ()
        {
                m = new My ();
                Thread t = new Thread (m.RunMe);
                t.Start ();
        }
 
        void OnApplicationQuit ()
        {
                m.Stop ();
        }
}
 
class My
{
        int i = 0;
        bool isRun = true;
 
        public void RunMe ()
        {
                while (isRun) { 
                        MonoBehaviour.print ("Run : " + i++);
                        Thread.Sleep (1000);
                }
        }
 
        public void Stop ()
        {
                isRun = false;
        }
}

黃彥霖 發表在 痞客邦 留言(0) 人氣()

// 精簡版
 
public class JPA03 {
    public static void main(String[] args) {
        java.util.Scanner s = new java.util.Scanner(System.in);
        int a,b,i;
        while(true){
            System.out.println("Input:");  
            if((i = (a = s.nextInt()))==999) break;            
            b = s.nextInt();            

黃彥霖 發表在 痞客邦 留言(2) 人氣()