java 基础


/**
* 求a的n次方。
* @param a
* @param n
* @return
*/
public double Factorial(double a,int n){
double v=1;//;
for(int i=0;i<n;i++){//
v*=a;
}
return v;
}
/**
* 递归实现n的阶乘。
* @param n
* @return
*/
public long fac(int n){
if(n==1){
return 1;
}else{
return n*fac(n-1);
}
}
/**
* 1!+2!+ ...+n!(阶乘和)
* @param a
* @param m
* @return
*/
public static long sumFactorial(int n) throws Exception {
long flag = 1;
long sum = 0;
for (int i = 1; i <= n; i++) {
flag *= i;
sum += flag;
if (flag < 0) throw new Exception("溢出了!!!在第" + i + "个阶乘出现负数");
if (sum < 0) throw new Exception("溢出了!!!在第" + i + "个阶乘之和出现负数");
}
return sum;
}



String.format:

System.out.println(String.format("%s","hi"));//hi
System.out.println(String.format("输出结果:[%s][%s]","hello","world"));//输出结果:[hello][world]
/**
* 员工编号
*/
public void printStr(){
for(int i = 1;i <= 1000;i++){
System.out.println(String.format("A%09d", i));//很好
}
}


/**
* 分别是a,b,c,d四个球队,现在四个球队进行比赛,有多少种对决方案。
* @return
*/
public String getVS(){
String[] teams = {"a","b","c","d"};
String result = "";
for(int i=0;i<teams.length;i++){
for(int j=0;j<i;j++){
result += teams[i]+" vs "+teams[j]+"\n";
}
}
return result;
}




/**
*帮助理解for循环。
**/
public class TestFor {
static boolean foo(char c) {
System.out.print(c);
return true;
}

public static void main(String[] argv) {
int i = 0;
for (foo('A'); foo('B') && (i < 2); foo('C')) {
i++;
foo('D');
}
}
}


java 单态

public class Singleton {

private static Singleton instance;//自己创建自己。
/**
* 生产该类对象的方法
* @return
*/
synchronized public static Singleton getSingletonInstance(){
if(instance==null){
instance=new Singleton();
}
return instance;
}
/**
* 私有构造方法(不让外部new 实例)
*/
private Singleton() {}
}

java 获取控制台数据
public void printMac(){
String result = new String();
String os = System.getProperty("os.name");
if (os != null && os.startsWith("Window")) {
try {
Process process = Runtime.getRuntime().exec(
"cmd.exe /c ipconfig /all");
BufferedReader read = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String s = read.readLine();
while (s != null) {
if (!s.equals("")) {
result += "\n";
result += s;
}
s = read.readLine();
}
System.out.println(result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}


	/**
* 字符串加1、补零
* @param str
* @return
*/
public static String getStrNum(String str){
String result="";
int src=Integer.parseInt(str);//转换成整型
src++;
result=Integer.toString(src);//加1后再转换成字符串
while(result.length()<str.length()){//如果加1后的长度小于原来字符串,前面补零
result="0"+result;
}
return result;
}

如何将Hashmap线程同步
Map map=Collections.synchronizedMap(Map m);



[code="java"][/code]
[quote]对象的Hashcode 并不是真正的对象在内存中的物理地址,但可以先理解为包装后的散列表。java中是不让用户知道对象的真正内存物理地址的。C/C++ 可以知道对象的物理内存地址。[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值