2013年3月29日星期五
java puzzers 94: shuffle
主要代码就是这一段:
Random rnd=new Random();
for (int i = 0; i < size; i++)
swap(array, i, rnd.nextInt(size));
这个算法是不公平的,Collections.shuffle用的是:
for (int i = size; i > 1; i--)
swap(arr, i - 1, rnd.nextInt(i));
其实就是(有点难看):
for (int i = 0; i < size; i++)
swap(array, i, i+rnd.nextInt(size-i));
java puzzers:"+","=="
匆匆看完,大部分都很有意思,不过看完都没什么印象,只有一个我现在还偶尔碰到.
puzzer 43:(排版稍微有点不一样)
运算符“+”和“==”的优先级,前者高.(不管是加法还是字符串连接)
puzzer 43:(排版稍微有点不一样)
很明显能看出pig!=dog,但是这个迷题的重点却在输出语句上,原文将“+pig==dog”显示在第二行,错误就更不容易看出来。public class AnimalFarm{public static void main(String[] args){final String pig = "length: 10";final String dog = "length: " + pig.length();System.out. println("Animals are equal: "+ pig == dog);}}
运算符“+”和“==”的优先级,前者高.(不管是加法还是字符串连接)
2013年3月28日星期四
ArrayList笔记
Jdk6:
1.初始化(源码):
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
下面是这个代号6260652中说到的bug代码示范:
List l = Arrays.asList(args);
System.out.println(l.toArray());
System.out.println(l.toArray(new Object[0]));
EXPECTED -
[Ljava.lang.Object;@10b62c9
[Ljava.lang.Object;@82ba41
ACTUAL -
[Ljava.lang.String;@10b62c9
[Ljava.lang.Object;@82ba41
[Ljava.lang.Object;@10b62c9
[Ljava.lang.Object;@82ba41
ACTUAL -
[Ljava.lang.String;@10b62c9
[Ljava.lang.Object;@82ba41
2.arrayList.toArray()内部调用的也是Arrays.copyOf
3.数组复制:
System.arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
4.
默认初始容量10, 扩容方法ensureCapacity(int minCapacity)是取
min{ minCapacity, (oldCapacity *3)/2+1 }
2013年3月27日星期三
queryBroadcastReceivers ACTION_MEDIA_MOUNTED
ACTION_MEDIA_MOUNTED等一系列与external media状态有关的broadcast actions都需要在intent-filter中设置scheme为file。
要查询这些事件的receiver,queryBroadcastReceivers的参数intent也必须匹配data的scheme属性.
implicit intents需要匹配:(参见intent 以及 intents-filters)
1.action
2.data (both URI and data type)
3.category
其中data的uri格式是:
scheme://host:port/path
在ACTION_MEDIA_*事件中,scheme为"file",host和port省略,path是external media的路径。
要查询这些事件的receiver,queryBroadcastReceivers的参数intent也必须匹配data的scheme属性.
implicit intents需要匹配:(参见intent 以及 intents-filters)
1.action
2.data (both URI and data type)
3.category
其中data的uri格式是:
scheme://host:port/path
在ACTION_MEDIA_*事件中,scheme为"file",host和port省略,path是external media的路径。
2013年3月25日星期一
RelativeLayout中的overlaping
今天用RelativeLayout,左边是上下两层textview,右边是一个checkbox,出现了overlapping,以前没怎么用过相对布局,才知道和FrameLayout一样也会overlapping.
具体:
前者不设置,或者与parent左对齐,这里没什么必要。
后者如下设置:
android:layout_alignParentRight="true"
后者只设置与parent右对齐:
android:layout_alignParentRight="true"
这样结果就会以后者为参照,左边的textview如果过长会自动换行。
具体:
前者不设置,或者与parent左对齐,这里没什么必要。
后者如下设置:
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/left"
结果以前者为参照,后者会被覆盖。
改为:
前者如下设置:
android:layout_toLeftOf="@+id/right"
结果以前者为参照,后者会被覆盖。
改为:
前者如下设置:
android:layout_toLeftOf="@+id/right"
后者只设置与parent右对齐:
android:layout_alignParentRight="true"
这样结果就会以后者为参照,左边的textview如果过长会自动换行。
2013年3月24日星期日
AsyncTask problem in HTC
出现下面的出错信息,只是因为我在doInBackground()里面对listview做了初始化,当然这好像也算ui操作,但是其它品牌的手机都没问题,只有htc有问题,这我就弄不明白了,记录一下吧。
-----------------------------------------------------------
E/AndroidRuntime( 6172): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime( 6172): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime( 6172): at android.os.AsyncTask$3.done(AsyncTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.FutureTask.setException(FutureTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.FutureTask.run(FutureTask.java)
E/AndroidRuntime( 6172): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java)
E/AndroidRuntime( 6172): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java)
E/AndroidRuntime( 6172): at java.lang.Thread.run(Thread.java)
E/AndroidRuntime( 6172): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
E/AndroidRuntime( 6172): at android.os.Handler.<init>(Handler.java)
E/AndroidRuntime( 6172): at android.view.GestureDetector$GestureHandler.<init>(GestureDetector.java)
E/AndroidRuntime( 6172): at android.view.GestureDetector.<init>(GestureDetector.java)
E/AndroidRuntime( 6172): at android.view.GestureDetector.<init>(GestureDetector.java)
E/AndroidRuntime( 6172): at android.view.GestureDetector.<init>(GestureDetector.java)
E/AndroidRuntime( 6172): at android.widget.ListView.<init>(ListView.java)
E/AndroidRuntime( 6172): at android.widget.ListView.<init>(ListView.java)
E/AndroidRuntime( 6172): at android.widget.ListView.<init>(ListView.java)
E/AndroidRuntime( 6172): at cn.wq.disableservice.MainActivity.b(Unknown Source)
E/AndroidRuntime( 6172): at cn.wq.disableservice.d.a(Unknown Source)
E/AndroidRuntime( 6172): at cn.wq.disableservice.d.doInBackground(Unknown Source)
E/AndroidRuntime( 6172): at android.os.AsyncTask$2.call(AsyncTask.java)
E/AndroidRuntime( 6172): ... 6 more
-----------------------------------------------------------
E/AndroidRuntime( 6172): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime( 6172): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime( 6172): at android.os.AsyncTask$3.done(AsyncTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.FutureTask.setException(FutureTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.FutureTask.run(FutureTask.java)
E/AndroidRuntime( 6172): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
E/AndroidRuntime( 6172): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java)
E/AndroidRuntime( 6172): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java)
E/AndroidRuntime( 6172): at java.lang.Thread.run(Thread.java)
E/AndroidRuntime( 6172): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
E/AndroidRuntime( 6172): at android.os.Handler.<init>(Handler.java)
E/AndroidRuntime( 6172): at android.view.GestureDetector$GestureHandler.<init>(GestureDetector.java)
E/AndroidRuntime( 6172): at android.view.GestureDetector.<init>(GestureDetector.java)
E/AndroidRuntime( 6172): at android.view.GestureDetector.<init>(GestureDetector.java)
E/AndroidRuntime( 6172): at android.view.GestureDetector.<init>(GestureDetector.java)
E/AndroidRuntime( 6172): at android.widget.ListView.<init>(ListView.java)
E/AndroidRuntime( 6172): at android.widget.ListView.<init>(ListView.java)
E/AndroidRuntime( 6172): at android.widget.ListView.<init>(ListView.java)
E/AndroidRuntime( 6172): at cn.wq.disableservice.MainActivity.b(Unknown Source)
E/AndroidRuntime( 6172): at cn.wq.disableservice.d.a(Unknown Source)
E/AndroidRuntime( 6172): at cn.wq.disableservice.d.doInBackground(Unknown Source)
E/AndroidRuntime( 6172): at android.os.AsyncTask$2.call(AsyncTask.java)
E/AndroidRuntime( 6172): ... 6 more
name前面的空格
不少流氓应用为了在默认排序下能排在前面,在应用名称前面加了空格。
它们加的空格不是我们常用的空格,而是non-breaking space.
常用空格在ascii中是32(十进制),十六进制是20。
non-breaking space在ascii中是160(十六进制是a0),这个用string的trim()方法无法去掉,只能replace掉。
它们加的空格不是我们常用的空格,而是non-breaking space.
常用空格在ascii中是32(十进制),十六进制是20。
non-breaking space在ascii中是160(十六进制是a0),这个用string的trim()方法无法去掉,只能replace掉。
2013年3月5日星期二
PendingIntent:defferent extra but same pendingIntent
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call
cancel() to remove it.
Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by
Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.
There are two typical ways to deal with this.
If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by
Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).
If you only need one PendingIntent active at a time for any of the Intents you will use, then you can alternatively use the flags
FLAG_CANCEL_CURRENT orFLAG_UPDATE_CURRENT to either cancel or modify whatever current PendingIntent is associated with the Intent you are supplying.SharedPreferences.getStringSet
public abstract Set<String> getStringSet (String key, Set<String> defValues)
Added in API level 11
Retrieve a set of String values from the preferences.
Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.
--------------------------------------------
Very strange,unbelievable.
We should make a copy of the set to store it like this:
Set<Str set=new HashSet<String>(setting.getStringSet("key", new HashSet<String>()));
订阅:
评论 (Atom)