顯示具有 [JAVA] 標籤的文章。 顯示所有文章
顯示具有 [JAVA] 標籤的文章。 顯示所有文章

2015年9月17日 星期四

Eclipse 設Java 1.8路徑

下載 Java SE Development Kit 8u51 

http://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html#jdk-8u51-oth-JPR


Eclipse 設Java 1.8路徑
/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home


2015年4月23日 星期四

[Java] Insert sort 插入排序法

Java:
public class InsertSort {
    int data[] = new int[6];
    int size = data.length;


    public static void main(String args[])
    {
        System.out.print("123");
        InsertSort is = new InsertSort();
        is.inputarr();
        is.showData();
        is.insert();
    }
 void inputarr()
    {
        int i;
        for( i = 0 ; i<size; i++)
        {
            try{
                System.out.print("please input:"+(i+1) +"element:");
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);
                data[i] = Integer.parseInt(br.readLine());
            }
            catch (Exception e)
            {

            }

        }
    }
 void showData()
    {
        int i;
        for(i = 0 ; i<size;i++)
        {
            System.out.print(data[i]+" ");

        }
        System.out.print("\n");
    }


時間複雜度為:O(n^2)
最壞及平均需較(n-1) + (n-2)+....3+2+1 = n(n-1)/ 2次

insert sort建議在錄結串列上使用,因為會常造成資料的大量搬移。

2014年3月19日 星期三

Android Webview javascript與Java 互動傳參數

重點是 要先設定

webview.setWebChromeClient(new WebChromeClient());
webview.addJavascriptInterface(new JavaScriptInterface(), "android");

於 HTML 的javascript加入

function executeFromObjCall(str)
{
    alert("execute from objective c call params:" + str);
            
}


然後在你的java檔加入

webview.loadUrl("javascript:executeFromObjCall('passValue');");


這樣就可以呼叫webview javascript檔

並由java傳值到javascript

 若要從javascript呼叫java的code的話

 我們先在javascript去呼叫某個function

如call callJavaFunction:


function callJavaFunction()
{                
   var str = window.android.callJavaMethod();
   alert(str);
}


在java code去新增

 public class JavaScriptInterface {
      public String callJavaMethod() {
           
       Log.i("info", "called from javascript execute in java");
       return "this is return params from java";
   }
}

JavaScriptInterface 是在上面有new JavaScriptInterface()的關係~ 

這樣就可以透過javascript呼叫java並回傳java的值到javascript了


2013年12月18日 星期三

Android APK 反編譯 為Java檔

若要參考別人的原始碼來學習,

一種方式就是將APK檔 反編譯回 JAVA檔,

此種方式需先下載兩個檔

分別是

dex2jar

JD-GUI

首先先將 手上的apk檔 把副檔名改成壓縮檔(zip or rar...),

再將此壓縮檔解壓縮出來,你會看見有一個classes.dex檔,

我們要將此.dex檔轉成jar檔再透過JD-GUI反編譯為原始的java程式碼。

下載完dex2jar後,到dos下執行指令

dex2jar.bat  classes.dex

接下來你會看見classes.dex.dex2jar.jar的檔生成在dex2jar.bat這個目錄之下。

接下來再去執行 JD-GUI 將這個jar檔開啟..所有的程式碼就出現囉..

2013年6月20日 星期四

ConcurrentMap使用

import java.util.concurrent.*;

public class TestThread{

    public static void main(String[] args)
    {
        ConcurrentMap myMap = new ConcurrentHashMap();
     Object o = new Object();
     o = myMap.putIfAbsent("1", "one");
     System.out.println( o );
     o = myMap.putIfAbsent("2", "two");
     System.out.println( o );
     o = myMap.putIfAbsent("3", "333");
     System.out.println( o );
     o = myMap.putIfAbsent("3", "3");
     System.out.println( o );
     System.out.println( myMap );
    }

}

回傳

null
null
null
333
{1=one, 3=333, 2=two}


使用putIfAbsent 可以發現若前面有key相同的話,則不會覆蓋之前的key value,

並且回傳333也就是之前相同key的value。

API文件:


putIfAbsent
public V putIfAbsent(K key,
            V value)
If the specified key is not already associated with a value, associate it with the given value. This is equivalent to
   if (!map.containsKey(key))
       return map.put(key, value);
   else
       return map.get(key);
except that the action is performed atomically.
Specified by:
putIfAbsent in interface ConcurrentMap
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key
Returns:
the previous value associated with the specified key, or null if there was no mapping for the key
Throws:
NullPointerException - if the specified key or value is null