Java编程手册

1.时间

long time = System.currentTimeMillis();
System.out.println(time);

2.字符串反向

  String s1="kjsfhfdhsj";
    s1 = new StringBuffer(s1).reverse().toString();//s1变为s1 = "jshdfhfsjk"

3.字符串转换

3.1字符串转换字符数组

char s[]=new char[str.length()];
String str="51525";
char s[] = str.toCharArray();

3.2字符串转换单个字符

char s = str.charAt(x);//s为str的第x个字符

3.3字符串转换整型

3.3.1使用parselint

String str="5";
int a=Integer.parseInt(str);//a变为5

3.3.2使用valueOf

String str="5";
int a=Integer.valueOf(str); //3.3.1和3.3.2作用一样

3.4字符串转换long型

long num = Long.parseLong(String str);

3.5字符串转换short型

short num=Short.parseShort(String str);

3.6字符串转换float型

float num = Float.parseFloat(String str);

3.7字符串转换double型

double num = Double.parseDouble(String str);

4.数组输出

int a[] = { 66, 61, 64, 5, 41, 56, 23, 63, 70, 34 };
System.out.println(Arrays.toString(a));

5.数组复制

5.1部分复制

int a[] = { 66, 61, 64, 5, 41, 56, 23, 63, 70, 34 };
int b[] = new int[11];
System.arraycopy(a, 0, b, 1, 10);// 没有第一项,及b[0]=0
//简而言之,System.arraycopy(原数组的数组名,开始复制的项,复制后的数组名,复制起始项,复制项个数)

5.2全部复制

int a[] = { 66, 61, 64, 5, 41, 56, 23, 63, 70, 34 };
int b[] = Arrays.copyOf(a, 10);

6.字符数组转换

6.1字符数组转换字符串

char data[]={'a','b','c'};
String s=new String(data);

7.整形转换

7.1整形到字符串

int i = 2133;
String str = Integer.toString(i);//str = "2133"

8.字符串数组转换

8.1字符串数组转换字符串

String str[] = {"abc", "bcd", "def"};
StringBuffer a = new StringBuffer();//使用append前的准备
for(int i = 0; i < str.length; i++)
     a. append(str[i]);
String s = a.toString();//s = abcbcddef

9.Long型转换

9.1 Long型转换字符型

String num = Long.toString(long n);

10.Short型转换

10.1 Short型转换字符型

String num = Short.toString(Short n);

11.Float型转换

11.1 Float型转换字符型

String num = Float.toString(Float n);

12.Double型转换

12.1 Double型转换字符型

String num = Double.toString(Double n);

13.随机数

double a = Math.random();//生成0.0,<=a<1.0

14.方法调用

14.1静态方法(static)

14.1.1 static void型

static void f(int x, int y){//f(参数),多个参数用“,”隔开
		int c, d;
		c = 5 * x;
		d = 15 % y;
		System.out.println(c + "\n" + d);
	}
	public static void main(String[] args){
		f(5, 6); 
	}
/**
* 相当于将
*		int c, d;
*		c = 5 * x;
*		d = 15 % y;
*		System.out.println(c + "\n" + d);
*这一段代码复制在main函数中
*/

14.1.2 static int型

static int f(int x, int y){ //f(参数),多个参数用“,”隔开
		int c, d;
		c = 5 * x;
		d = 15 % y;
		return c+d;
	}
	public static void main(String[] args){
		int a=f(6, 8);
		System.out.println(f(5, 6));
		System.out.println(a);
	}
/**
*相当于f函数输出一个整型,值为c+d
*/

14.1.3static boolean型

static boolean f(int x, int y){
		boolean a = false;
		int c, d;
		c = 5 * x;
		d = 15 % y;
		if (c < d){
			a = true;
		}
		return a;
	}

14.1.4其他的static方法调用

static [方法类型] [方法名] ([参数类型]参数){//多个参数之间用",“隔开
// 方法体
return [值,一定和方法类型相同的值];
}
public static void main(String[] args){
[方法类型] [你想用的值的名字] = [方法名] (参数);//同样,多个用”,"隔开
//或者直接[方法名] (参数),其值为return后的值
}

猜你喜欢

转载自blog.csdn.net/FishPotatoChen/article/details/83187495