JAVA:
char型数据和整型数据运算结果的精度是int
例如: byte k=18;
那么: ‘H’+k; 结果是int型
10>20-17 相当于 10>(20-17)
C++ java 有布尔型而C没有
Java :?????????????????
double g=(-15)%5.2; -4.6
double h=15%(-5.2); -------à 4.6 C中不合法
java 的float型要加f C++ ,C不用
l 对于byte或short型数据,a<<n的运算结果是int型精度
运算时,计算系统首先将a升级为int型数据,对于正数将高位用0填充;负数用1填充,然后再进行移位运算
Java有包的概念:包内除了private 不能访问其它的都行!
不是一个包内但是子类能访问 protected public
**********************************************************************
Public 包类 子类 本类 别的类 protected包类,子类,本类限制 default 加了包类 本类 private 只有本类 只要一个满足就行了
TreeMap:红黑树实现的,它也是hashmap形式但是
//制作比较器
class CollatorComparator implements Comparator<Integer> {
//Collator collator =Collator.getInstance();
public intcompare(Integer element1, Integer element2) {
// CollationKey key1 =collator.getCollationKey(element1.toString());
//CollationKey key2 =collator.getCollationKey(element2.toString());
returnelement1.compareTo(element2);
}
}
//生成Treemap
CollatorComparator comparator = new CollatorComparator();
TreeMap<Integer,collegestudent> tmp = newTreeMap<Integer,collegestudent>(comparator);
//TreeMap中放对象
for(collegestudentco:ar_cost){
tmp.put(co.getMath(),co);
}
//遍历输出TreeMap
Iterator<Integer> iterator_2 =tmp.keySet().iterator();
while (iterator_2.hasNext()) {
Object key = iterator_2.next();
System.out.println("tmp.get(key) is :" +tmp.get(key).getMath());
}
//也可以利用HashMap的对象,直接一次性放到TreeMap中:即先利用HashMap来存放,然后放在里面。这种情况比较适合利用HashMap来实现快速查询,如果需要有序输出就可以利用TreeMap
内部类的实现:
//传参数给内部类的形式;内部类是局部的。
abstract classGeometry{
publicabstractdoublegetArea(); }
//class Circle extends Geometry{
// doubler;
///Circle(double r){this.r=r;}
//public double getArea(){return 3.14*r*r;} }
public class test{
public Geometry calculate(final int r){
return newGeometry(){
public double getArea(){return 3.14*r*r;}
};
}
public static void main(String[]args)
{ //Circle c=newCircle(3);
tests=newtest();
System.out.println("The area of geometry is: "+s.calculate(4).getArea());
}
}
1. public class Outer {
2. public static void main(String[] args) {
3. Outer outer = new Outer();
4. Inner inner = outer.getInner("Inner", "gz");
5. System.out.println(inner.getName());
6. }
7.
8. public Inner getInner(final String name, String city) {
9. return new Inner(name, city) {
10. private String nameStr = name;
11.
12. public String getName() {
13. return nameStr;
14. }
15. };
16. }
17. }
18.
19. abstract class Inner {
20. Inner(String name, String city) {
21. System.out.println(city);
22. }
23.
24. abstract String getName();
25. }
//详细一点的信息请看:http://android.blog.51cto.com/268543/384844
Java中一般对象是放在堆中,而栈中一般放基本类型数据和堆中对象的地址
public interface CharStorage {
//常量 默认添加static
public static int k=0;
// 方法默认添加public abstract
voidput(char c);
}
重写equals方法后最好重写hashCode方法,否则两个等价对象可能得到不同的hashCode,这在集合框架中使用可能产生严重后果;
public boolean equals(Objectother){ }
public int hashCode(){ }
hashcode是用于散列数据的快速存取,如利用HashSet/HashMap/Hashtable类来存储数据时,都是根据存储对象的hashcode值来进行判断是否相同的。
Hashcode原理 HashSet用法?????????????????????看视频
String StringBuffer
元字符 | 在正则表达式中的写法 | 意义 |
. | “.” | 代表任何一个字符 |
\d | “\\d” | 代表0至9的任何一个数字 |
\D | “\\D” | 代表任何一个非数字字符 |
\s | “\\s” | 代表空格类字符, ‘\t’、’\n’、 ‘\x0B’、 ‘\f’、 ‘\r’ |
\S | “\\S” | 代表非空格类字符 |
\w | “\\w” | 代表可用于标识符的字符 |
\W | “\\W” | 代表不能用于标识符的字符 |
带限定符号的模式 | 意义 |
X? | X出现0次或1次 |
X* | X出现0次或多次 |
X+ | X出现1次或多次 |
X{n} | X恰好出现n次 |
X{n,} | X至少出现n次 |
X{n,m} | X出现n次至m次 |
容器:
Vector<E>,ArrayList<E>都是数组实现形式,但是Vector考虑了同步(多线程)
Set接口实现的类特征是:不能重复(HashSetTreeSet) List 接口实现的类特征是:可重复(ArrayList,Vector,LinkedList)
遍历一般有四种方法: 1.size 2.iterator 3.toArray 4 for
http://java.chinaitlab.com/advance/813917.html
文件处理:
File可以对于操作文件整体信息还有处理
InputstreamFile OutputstreamFile 处理文件的字节读写操作
ReaderFile WriterFile 处理文件的字符读写操作
BufferedReader 缓存类配合ReaderFile进行逐行读出readLine()
l wait()、notify()和notifyAll()都是Object类中的final方法,被所有的类继承,且不允许重写的方法。
l 当一个线程使用的同步方法中用到某个变量,而此变量又需要其它线程修改后才能符合本线程的需要,那么可以在同步方法中使用wait()方法。
l 使用wait()方法可以中断方法的执行,使本线程等待,暂时让出CPU的使用权,并允许其它线程使用这个同步方法。
Java中主线程结束后可能其它线程还在执行。
package concurrency;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
class Athlete implements Runnable {
private final int id;
private Game game;
public Athlete(int id, Game game) {
this.id = id;
this.game = game;
}
public boolean equals(Object o) {
if (!(o instanceof Athlete))
return false;
Athlete athlete = (Athlete) o;
return id == athlete.id;
}
public String toString() {
return "Athlete<" + id + ">";
}
public int hashCode() {
return new Integer(id).hashCode();
}
public void run() {
try {
game.prepare(this);
} catch (InterruptedException e) {
System.out.println(this + " quit thegame");
}
}
}
public class Game implements Runnable {
private Set<Athlete> players = new HashSet<Athlete>();
private boolean start = false;
public void addPlayer(Athlete one) {
players.add(one);
}
public void removePlayer(Athlete one) {
players.remove(one);
}
public Collection<Athlete> getPlayers() {
return Collections.unmodifiableSet(players);
}
public void prepare(Athlete athlete) throws InterruptedException {
System.out.println(athlete + " ready!");
synchronized (this) {
while (!start)
wait();
if (start)
System.out.println(athlete + " go!");
}
}
public synchronized void go() {
notifyAll();
}
public void ready() {
Iterator<Athlete> iter =getPlayers().iterator();
while (iter.hasNext())
new Thread(iter.next()).start();
}
public void run() {
start = false;
System.out.println("Ready......");
System.out.println("Ready......");
System.out.println("Ready......");
ready();
start = true;
System.out.println("Go!");
go();
}
public static void main(String[] args) {
Game game = new Game();
for (int i = 0; i < 10; i++)
game.addPlayer(new Athlete(i, game));
new Thread(game).start();
}
}
结果:
Ready......
Ready......
Ready......
Athlete<0> ready!
Athlete<1> ready!
Athlete<2> ready!
Athlete<3> ready!
Athlete<4> ready!
Athlete<5> ready!
Athlete<6> ready!
Athlete<7> ready!
Athlete<8> ready!
Athlete<9> ready!
Go!
Athlete<9> go!
Athlete<8> go!
Athlete<7> go!
Athlete<6> go!
Athlete<5> go!
Athlete<4> go!
Athlete<3> go!
Athlete<2> go!
Athlete<1> go!
Athlete<0> go!