Increment and decrement front and rear positions
Some very boring questions will have multiple increments or decrements.
E.g
int a = 2 ;
int b = 3 ;
a= ++a+b+++b+++b++;
//问你a,b的值,是不是想问候出题者祖宗。
/**
首先看等号右边,++a+b+++b+++b++
先看怎么断句, ++a + b++ + b++ +b++
a的自增在前,那么先加一,a为3
再+b++ ,b的自增在后请先忽略自增,+b是加上3,然后b的值此时变成了4
再+b++ ,b的自增在后请先忽略自增,+b是加上4,然后b的值此时变成了5
再+b++ ,b的自增在后请先忽略自增,+b是加上5,然后b的值此时变成了6
*/
To summarize the law, if the increment is first, then its priority will increase. If the increment is behind the value, then it will have no effect on the calculation. If I customize the priority, please ignore the increment first. Do other operations, but once the increment sign is over, the value will be incremented by one.
Type conversion
float f = 5 / 2 ;
//问f的值, f是float ,等号右边又是整型,那么除出来结果是2,然后在类型转化为float,那么f为 2.0F.
Ask you how to find the length of an int type number.
//方法1:
int num = 12345 ; //假设我想知道的数是该数,但是不知道长度。
//短除10,一直除到0,同时每次加一
int index = 0 ;
while(num != 0){
num /= 10 ;
index ++ ;
}
//方法2
int num = 12345 ; //假设我想知道的数是该数,但是不知道长度。
//利用字符串有长度属性然后用个空串来拼接它,然后.length获取长度
String s = "";//给个空串
s += num ;//利用字符串的拼接
int len = s.lengh();
System.out.println("长度为:" + len);
Concatenation of strings
String s = "a";
s += 2 + 5 ;
//问你s 为多少,这个是字符的拼接,s就是a25
The short-circuit effect of logical operators
int i = -124;
if( i > 0 && i++){
System.out.println("i");
}
//这里有短路效应,因为 &&前面条件不成立,所以 i++不会执行,i的值是 -124
Remove 0 in the array to get a new array
public class Demo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* 如何将一个数组中的0全部删除,然后赋值给一个新的数组保存没有0的数组
例如原数组 a = { 1,0,4,8,0,0,5,7,9};
保存的新数组 b ={ 1,4,8,5,7,9}
**/
int [] a = {
1,0,4,8,0,0,5,7,9};
int [] b = t1(a);
System.out.println(Arrays.toString(b));
}
public static int [] t1(int []a) {
int [] b ;
int len = 0 ; //为a数组没有0的长度
int index = 0 ; //保存新数组的下标
for( int i = 0 ; i < a.length ; i++) {
if( a[i] != 0) {
len ++ ;
}
}
b = new int [len] ;
for( int j = 0 ; j < a.length ; j++) {
if( a[j] != 0) {
b[index] = a[j] ;
index ++ ;
}
}
return b ;
}
}