贴一些演示简单API的代码

有部分使用不正确,不要模仿:

/**

* enum

*/

package com;

/**

* @author Administrator

*

*/

public class EnumTest {

enum Week {

Mon, Tue, Wed, Thu, Fri, Sat, Sun

}

/**

* @param args

*/

public static void main(String[] args) {

System.out.println(Week.valueOf("Mon"));

//

System.out.println(Week.Mon);

System.out.println(Week.Sun);

Week[] w = Week.values();

for (Week we : w) {

System.out.println(we);

}

switch (Week.Sun) {

case Mon:

System.out.println("1");

break;

default:

System.out.println("default");

}

System.out.println(Week.Mon.compareTo(Week.Fri));

System.out.println(Coffee.small);

System.out.println(Coffee.small.getSize());

Coffee[] coffee = Coffee.values();

for(Coffee c: coffee) {

System.out.println(c);

System.out.println(c.getSize());

}

System.out.println(Week.valueOf("Mon1"));

}

}

enum Coffee {

small(8), middle(10), large(12);

private int size;

Coffee(int size) {

this.size = size;

}

public int getSize() {

return size;

}

}

Exception in thread "main" java.lang.IllegalArgumentException: No enum const class com.EnumTest$Week.Mon1

at java.lang.Enum.valueOf(Enum.java:196)

at com.EnumTest$Week.valueOf(EnumTest.java:1)

at com.EnumTest.main(EnumTest.java:48)

Mon

Mon

Sun

Mon

Tue

Wed

Thu

Fri

Sat

Sun

default

-4

small

8

small

8

middle

10

large

12

/**

*

*/

package com;

/**

* @author Administrator

*

*/

public class ForTest {

public static void main(String[] args) {

for (int i = 0, j = 10; i < 10;) {

System.out.println(i + j);

i++;

}

}

}

10

11

12

/**

*

*/

package com;

import java.util.ArrayList;

import java.util.List;

/**

* @author Administrator

*

*/

public class TestWildcards {

public static void main(String[] args) {

List<Integer> myList = new ArrayList<Integer>();

Bar bar = new Bar();

bar.doInsert(myList);

}

}

class Bar {

void doInsert(List<? extends Object> list) {

list.contains(new Dog());

// 说不清楚具体是什么 可添加多种对象而造成危险 比如 Cat Dog等。

list.add(new Object); // compile error

}

}

class Bar2 {

void doInsert(List<? super String> list) {

list.contains(new Dog());

// string is a ? 合理

list.add("test");

}

}

import java.util.Comparator;

import java.util.PriorityQueue;

/**

*

*/

/**

* @author Administrator

*

*/

public class PriorityQueueTest {

static class PQsort implements Comparator<Integer> { // inverse sort

public int compare(Integer one, Integer two) {

return two - one; // unboxing

}

}

public static void main(String[] args) {

int[] ia = { 1, 5, 3, 7, 6, 9, 8 }; // unordered data

PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>(); // use

// natural

// order

for (int x : ia)

// load queue

pq1.offer(x);

for (int x : ia)

// review queue

System.out.print(pq1.poll() + " ");

System.out.println("");

PQsort pqs = new PQsort(); // get a Comparator

PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs); // use

// Comparator

for (int x : ia)

// load queue

pq2.offer(x);

System.out.println("size " + pq2.size());

System.out.println("peek " + pq2.peek());

System.out.println("size " + pq2.size());

System.out.println("poll " + pq2.poll());

System.out.println("size " + pq2.size());

for (int x : ia)

// review queue

System.out.print(pq2.poll() + " ");

}

}

1 3 5 6 7 8 9

size 7

peek 9

size 7

poll 9

size 6

8 7 6 5 3 1 null

/**

*

*/

package com;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

/**

* @author Administrator

*

*/

public class CollectionsTest {

/**

* @param args

*/

public static void main(String[] args) {

List list = new ArrayList();

list.add("a");

list.add("b");

list.add("c");

list.add("a23");

Collections.sort(list);

int i = Collections.binarySearch(list, "a23");

System.out.println(list);

PQsort pq = new PQsort();

Collections.sort(list,pq);

int j = Collections.binarySearch(list, "a23");

System.out.println(i);

System.out.println(j);

int m = Collections.binarySearch(list, "a23", pq);

System.out.println(m);

System.out.println(list);

}

}

class PQsort implements Comparator<String> { // inverse sort

public int compare(String one, String two) {

return two.compareTo(one); // unboxing

//return one.compareTo(two); // unboxing

}

}

[a, a23, b, c]

1

-1

2

[c, b, a23, a]

/**

*

*/

package file;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.Serializable;

/**

* @author Administrator

*

*/

public class FileTest implements Serializable {

public static void main(String[] args) {

String filePath = "d://test";

File file = new File(filePath);

file.mkdir();

File fileName = new File("d://test//t.dat");

try {

fileName.createNewFile();

} catch (IOException e)

分享到:
评论
ruilin215
  • 浏览: 234618 次
  • 性别: Icon_minigender_2
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

你可能感兴趣的:(java,C++,c,C#,sun)