import java.util.*;
public class Ex_PriorityQueue_1{
public static void main(String args[]){
Comparator<String> c = new Comparator<String>(){
public int compare(String a , String b){
return a.compareTo(b)*-1;
}
};
PriorityQueue<String> pq = new PriorityQueue<String> (3,c);
pq.offer("c");
pq.offer("a");
pq.offer("b");
String s;
while((s = pq.poll())!= null){
System.out.println(s+" ");
}
}
}
PriorityQueue
關於Comparator別人哦論點
http://give.pixnet.net/blog/post/11346747-how-to-sort-objects-in-java-%28%E6%8E%92%E5%BA%8Fjava%E7%89%A9%E4%BB%B6%29
自己的小練習, 如有侵權, 被我不小心引用沒標名出處或是不想被我標示出來, 趕快跟我講, 有時候一忙會忘記看這個, 有問題或是有更好的辦法更要跟我講, 如果有什麼覺得可以更積極的, 也希望可以小力的酸我, 我玻璃心阿
總網頁瀏覽量
2015年9月28日 星期一
[Java] HashMap 練習
import java.util.*;
public class Ex_HashMap{
public static void main(String args[]){
HashMap map = new HashMap();
map.put("a","abcd");
map.put(new Integer(200), new Integer(2));
map.put(new Object(),"object");
map.put(null, null);
System.out.println(map.toString());
System.out.println("key =A: "+ map.get("a"));
System.out.println("key =B: "+ map.get("B"));
System.out.println("key =new Integer(100): "+map.get(200));
}
}
{null=null, a=abcd, 200=2, java.lang.Object@15db9742=object}
key =A: abcd
key =B: null
key =new Integer(100): 2
public class Ex_HashMap{
public static void main(String args[]){
HashMap map = new HashMap();
map.put("a","abcd");
map.put(new Integer(200), new Integer(2));
map.put(new Object(),"object");
map.put(null, null);
System.out.println(map.toString());
System.out.println("key =A: "+ map.get("a"));
System.out.println("key =B: "+ map.get("B"));
System.out.println("key =new Integer(100): "+map.get(200));
}
}
{null=null, a=abcd, 200=2, java.lang.Object@15db9742=object}
key =A: abcd
key =B: null
key =new Integer(100): 2
[Java] Queue中poll和peek的差別
import java.util.*;
public class Ex_Queue_2{
public static void main(String args[]){
Queue q1 = new LinkedList();
q1.offer("First");
Queue q2 = new LinkedList();
q2.offer("Second");
System.out.println(q1.poll());
System.out.println(q1.toString()+"\n");
System.out.println(q2.peek());
System.out.println(q2.toString());
}
}
First
[]
Second
[Second]
poll()和peek()差別在於poll會將元素移除, 而peek只會顯示
public class Ex_Queue_2{
public static void main(String args[]){
Queue q1 = new LinkedList();
q1.offer("First");
Queue q2 = new LinkedList();
q2.offer("Second");
System.out.println(q1.poll());
System.out.println(q1.toString()+"\n");
System.out.println(q2.peek());
System.out.println(q2.toString());
}
}
First
[]
Second
[Second]
poll()和peek()差別在於poll會將元素移除, 而peek只會顯示
[Java] Queue
import java.util.*;
public class Ex_Queue{
public static void main(String args[]){
Queue q = new LinkedList();
q.offer("First");
q.offer("Second");
q.offer("Third");
Object o;
System.out.println(q.toString());
while((o=q.poll())!=null){
String s = (String)o;
System.out.println(s);
}
System.out.println(q.toString());
}
}
[First, Second, Third]
First
Second
Third
[]
因為每次用poll把queue內的元素取出,
所以最後是空的.
public class Ex_Queue{
public static void main(String args[]){
Queue q = new LinkedList();
q.offer("First");
q.offer("Second");
q.offer("Third");
Object o;
System.out.println(q.toString());
while((o=q.poll())!=null){
String s = (String)o;
System.out.println(s);
}
System.out.println(q.toString());
}
}
[First, Second, Third]
First
Second
Third
[]
因為每次用poll把queue內的元素取出,
所以最後是空的.
[Java] ArrayList
import java.util.*;
public class Ex_ArrayList{
public static void main(String args[]){
ArrayList as = new ArrayList();
as.add("CCCD");
as.add("ACDE");
as.add("ADEF");
ListIterator it = as.listIterator();
while(it.hasNext()){
int index = it.nextIndex();
String data = (String) it.next();
System.out.println(index+" "+data+"\n");
}
}
}
0 CCCD
1 ACDE
2 ADEF
public class Ex_ArrayList{
public static void main(String args[]){
ArrayList as = new ArrayList();
as.add("CCCD");
as.add("ACDE");
as.add("ADEF");
ListIterator it = as.listIterator();
while(it.hasNext()){
int index = it.nextIndex();
String data = (String) it.next();
System.out.println(index+" "+data+"\n");
}
}
}
0 CCCD
1 ACDE
2 ADEF
2015年9月27日 星期日
[Java] IO test
import java.io.*;
public class Ex_File{
public static void main(String args[]){
File f = new File("NewFile.txt");
System.out.println("Data exist: "+ f.exists());
if(!f.exists()){
System.out.print("Use createNewFile() \n");
}
try{
System.out.println(f.createNewFile());
}catch(IOException e){
System.out.println(e);
}
System.out.println("Data exist: "+ f.exists());
}
}
---------------------------------------
Data exist: false
Use createNewFile()
true
Data exist: true
public class Ex_File{
public static void main(String args[]){
File f = new File("NewFile.txt");
System.out.println("Data exist: "+ f.exists());
if(!f.exists()){
System.out.print("Use createNewFile() \n");
}
try{
System.out.println(f.createNewFile());
}catch(IOException e){
System.out.println(e);
}
System.out.println("Data exist: "+ f.exists());
}
}
---------------------------------------
Data exist: false
Use createNewFile()
true
Data exist: true
2015年9月23日 星期三
[C] Mod 模除
模除是一種不具交換性的二元運算。
程式語言 : 運算子
C : %
C++ : %
Java : %
JavaScript : %
PHP : %
Microsoft Excel : MOD() (函數)
SQL : mod() (函數)
但是在C鐘的%只能對於int型態作運算,
雖然C++不設址限制.
原理應該是因為取餘數數學上只取整數餘數,
如果要對於浮點數的取餘數原理上是不通的.
但是還是要取的話就會使用modf達成,
#include <stdio.h> /* printf */
#include <math.h> /* fmod */
#include <cstdlib>
int main ()
{
printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3 , 2) );
printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5, 4.2) );
system("pause");
return 0;
}
fmod of 5.3 / 2 is 1.300000
fmod of 18.5 / 4.2 is 1.700000
---------------------------------------------------------------
https://zh.wikipedia.org/wiki/%E5%90%8C%E9%A4%98
程式語言 : 運算子
C : %
C++ : %
Java : %
JavaScript : %
PHP : %
Microsoft Excel : MOD() (函數)
SQL : mod() (函數)
但是在C鐘的%只能對於int型態作運算,
雖然C++不設址限制.
原理應該是因為取餘數數學上只取整數餘數,
如果要對於浮點數的取餘數原理上是不通的.
但是還是要取的話就會使用modf達成,
#include <stdio.h> /* printf */
#include <math.h> /* fmod */
#include <cstdlib>
int main ()
{
printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3 , 2) );
printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5, 4.2) );
system("pause");
return 0;
}
fmod of 5.3 / 2 is 1.300000
fmod of 18.5 / 4.2 is 1.700000
---------------------------------------------------------------
https://zh.wikipedia.org/wiki/%E5%90%8C%E9%A4%98
訂閱:
文章 (Atom)