Java随记,
分享于 点击 36820 次 点评:253
Java随记,
Runtime几点用途
public class testruntime {
public static void main(String[] args) {
// TODO Auto-generated method stub
Runtime rt = Runtime.getRuntime();
System.out.println("处理器数量:"+rt.availableProcessors());
System.out.println("可用内存:"+rt.freeMemory());
}
}
Scanner读取文件内容
public class testScannerfile {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Scanner sc = new Scanner(new File("test1.txt"));
while(sc.hasNextLine()){
System.out.println(sc.nextLine());
}
}
}
String CompareTo方法
其他更多string的方法查看java疯狂讲义249页。最好查看API
stringbuilder的几个方法
public class testSringbuilder {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuilder str = new StringBuilder("hello ");
//结尾添加
str.append("w");
//插入
str.insert(7, "o");
//替换
str.replace(7, 8, "or");
//反转
str.reverse();
//设置字符串的长度
str.setLength(5);
System.out.println(str);
}
}
Random几种简单用法
public class TestRand {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rand = new Random();//伪随机
Random rand2 = new Random(System.currentTimeMillis());
//生成随机数,可以是int,float,double等都可
//生成0-10之间的整数
System.out.println(rand.nextInt(10));
System.out.println(rand2.nextInt(5));
ThreadLocalRandom val = ThreadLocalRandom.current();
System.out.println(val.nextDouble(2,5));
}
}
相关文章
- 暂无相关文章
用户点评