int a = 1;
a = a++;
1,取出a的值1作为a++表达式的值(a++表达式等于1)
2,a增加1变为2
3,执行赋值运算,a++表达式的值再赋给a,a又成为了1.
-----------------------------------------------
package pack01;
public class HexBie {
public static void main(String[] args){
int a = 1;
a = a++;
System.out.println(a);
int b = 1;
a = 1;
b = a++;
System.out.println(b);
a = 1;
for(int i=0;i<1000;i++){
a = a++;
}
System.out.println(a);
}
}
---------
1
1
1
-----------------------------------------------
数组拷贝函数
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Arrays.copyOf(int[] original, int newLength) //6.0以后才有
-----------------------------------------------
package pack01;
import java.util.Arrays;
public class HexBie {
public static void main(String[] args){
int[] arr = {1, 2, 3, 4};
int[] arr2 = new int[2];
System.arraycopy(arr, 1,arr2,0, 2);
for(int i:arr2){
System.out.println(i);
}
int[] arr3 = new int[2];
arr3 = Arrays.copyOf(arr, 2);
for(int i:arr3){
System.out.println(i);
}
}
}
-------
2
3
1
2
-----------------------------------------------
数组的扩展问题:
java中数组的大小是固定的,数组对象是不能扩展的,可以利用数组复制的方法实现数组扩展
-----------------------------------------------
package pack01;
import java.util.Arrays;
public class HexBie {
public static void main(String[] args){
String[] arr1 = {"how","old"};
String[] temp = Arrays.copyOf(arr1, 5);
String[] arr2 = temp;
arr2[2] = "are";
arr2[3] = "you";
arr2[4] = "?";
for(String s:arr2){
System.out.print(s+" ");
}
}
}
----------
how old
are you
?
-----------------------------------------------
package pack01;
import java.util.Arrays;
public class HexBie {
public static void main(String[] args){
char[] arr1 = {'a','b'};
System.out.println(arr1);
//字符数组以字符串的形式输出
System.out.println(Arrays.toString(arr1));
int[] arr2 = {1, 2};
System.out.println(arr2);
//数字数组输出的是数组对象内存地址
System.out.println(Arrays.toString(arr2));
String[] arr3 = {"a","b"};
System.out.println(arr3);
//字符串数组输出的是数组对象的内存地址
System.out.println(Arrays.toString(arr3));
}
}
----------
ab
[a, b]
[I@de6ced
[1, 2]
[Ljava.lang.String;@c17164
[a, b]
-----------------------------------------------
/**
* 统计一个字符在字符串中的所有位置
*/
package pack01;
import java.util.Arrays;
public class HexBie {
public static void main(String[] args){
String str = "统计一个字符在字符串中的所有位置";
int[] arr = countAll(str, '字');
System.out.println(Arrays.toString(arr));
}
private static int[] countAll(String str, char c) {
int arr[]={};
for(int i=0;i<str.length();i++){
char c1 = str.charAt(i);
if(c == c1){
arr = Arrays.copyOf(arr, arr.length+1);
arr[arr.length-1] = i;
}
}
return arr;
}
}
------
[4, 7]
-----------------------------------------------
常见的排序方法:
正排序:由大到小。 反排序:由小到大。
1,冒泡法:
2,选择法:
3,插入法:
----------------------------------------------------
/**
*常用的排序
*/
package pack01;
import java.util.Arrays;
public class HexBie {
public static void main(String[] args){
int[] arr1 = {2,4,9,6,3,7,1,5,8};
int[] arr2 = {2,4,9,6,3,7,1,5,8};
int[] arr3 = {2,4,9,6,3,7,1,5,8};
maoPao(arr1); //冒泡法
xuanZe(arr2); //选择法
chaRu(arr3); //插入法
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr3));
}
//将要插入的数和前面的比较
private static void chaRu(int[] arr) {
int t,j;
for(int i=0;i<arr.length;i++){
t = arr[i];
for(j=i-1;j>=0&&t<arr[j];j--){
arr[j+1] = arr[j]; //向后移
}
arr[j+1] = t;
}
}
//将最小的选择出来放在前面,依次类推
private static void xuanZe(int[] arr) {
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
}
}
}
//两个两个交换,就像冒泡泡一样
private static int[] maoPao(int[] arr) {
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length-i-1;j++){
if(arr[j+1]<arr[j]){
arr[j+1] = arr[j+1]^arr[j];
arr[j] = arr[j]^arr[j+1];
arr[j+1] = arr[j+1]^arr[j];
}
}
}
return arr;
}
}
----------------
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
----------------------------------------------------
方法的递归调用:
递归调用不断在栈内存中声明变量,所以递归效率较低。
----------------------------------------------------
/**
* n的阶乘
*/
package pack01;
import java.util.Arrays;
public class HexBie {
public static void main(String[] args){
int y = f(4);
System.out.println(y);
}
public static int f(int n){
if(n==0) return 1;
int y = f(n-1)*n;
return y;
}
}
----------------------------------------------------