`
ileson
  • 浏览: 211526 次
  • 性别: Icon_minigender_1
  • 来自: 河南省
社区版块
存档分类
最新评论

java 基础

    博客分类:
  • 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);





      
引用
对象的Hashcode 并不是真正的对象在内存中的物理地址,但可以先理解为包装后的散列表。java中是不让用户知道对象的真正内存物理地址的。C/C++ 可以知道对象的物理内存地址。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics