public static String myToString(int[] array) {
String ret = "[";
for(int i = 0;i < array.length;i++){
ret += array[i];虽然没有问题,但是不建议在循环里面使用string进行拼接,建议使用StringBuffer或者StringBuilder,后续介绍呀
if(i != array.length-1){
ret += ",";
}
}
return ret +="]";
}
public static void main(String[] args) {
int[] array = {1,2,3};
String ret = myToString(array);
System.out.println(ret);
}
①for循环完成
public static void main(String[] args) {
int[] array1 = {1,2,3,4};
System.out.println("array1:"+Arrays.toString(array1));
int[] array2 = new int[array1.length];
for (int i = 0;i < array1.length;i++){
array2[i] = array1[i];
}
System.out.println("拷贝结束,修改array2[0]的值");
array2[0]=10;
System.out.println("array2:"+Arrays.toString(array2));
}
②Arrays.copyOf(原来的数组,拷贝的长度)
public static void main(String[] args) {
int[] array1 = {1,2,3,4};
int[] array2 = Arrays.copyOf(array1,array1.length);//也可以指定个数拷贝爱,比如把array.length改成2就是拷贝前两个
System.out.println("array1:"+Arrays.toString(array1));
System.out.println("拷贝结束,修改array2[0]的值");
array2[0]=10;
System.out.println("array2:"+Arrays.toString(array2));
}
③Arrays.copyOfRange():拷贝从from到to下标的拷贝
public static void main(String[] args) {
int[] array1 = {1,2,3,4};
int[] array2 = Arrays.copyOfRange(array1,1,3);//[from,to)
System.out.println("array1:"+Arrays.toString(array1));//array1:[1, 2, 3, 4]
System.out.println("拷贝结束,修改array2[0]的值");
array2[0]=10;
System.out.println("array2:"+Arrays.toString(array2));//array2:[10, 3]
}
④System.arraycopy()
native方法是由c\c++实现的,优点是快(无法自己实现)
public static void main(String[] args) {
int[] array1 = {1,2,3,4};
int[] array2 = new int[array1.length];
System.arraycopy(array1,0,array2,0,array1.length);
System.out.println("array1:"+Arrays.toString(array1));
System.out.println("拷贝结束,修改array2[0]的值");
array2[0]=10;
System.out.println("array2:"+Arrays.toString(array2));
}
⑤clone()
public static void main(String[] args) {
int[] array1 = {1,2,3,4};
int[] array2 = array1.clone();
System.out.println("array1:"+Arrays.toString(array1));
System.out.println("拷贝结束,修改array2[0]的值");
array2[0]=10;
System.out.println("array2:"+Arrays.toString(array2));
}
总结:以上代码实现的拷贝均是浅拷贝,但是评判一个拷贝是浅拷贝还是深拷贝是要看代码的,不是看用的哪个具体的方法(没有具体说明的情况下,都是针对对象的拷贝说的)
深拷贝:修改拷贝之后的值不影响之前的值
浅拷贝:修改拷贝之后的值影响之前的值
public static double avg(int[] array){
int sum = 0;
for(int i = 0; i<array.length;i++){
sum += array[i];
}
return sum *1.0/array.length;
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
System.out.println(avg(array));
}
public static int fundVal(int[] array,int key){
for(int i = 0 ;i<array.length;i++){
if(array[i]==key){
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {1,2,34,5,6};
int key = 2;
System.out.println(fundVal(array,key));
}
public static int binarySearch(int[] array,int key){
int left = 0;
int right = array.length-1;
while(left <= right){
int mid = (left+right)/2;
if(array[mid] == key){
return mid;
}else if(array[mid] < key){
left = mid + 1;
}else{
right = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {1,2,3,4};
Arrays.sort(array);//数组排序
System.out.println(binarySearch(array,2));
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,61,7,8};
Arrays.sort(array);
int index = Arrays.binarySearch(array,7);
System.out.println(index);
}
public static void bubbleSort(int[] array){
//i表示趟数
for(int i = 0; i < array.length - 1;i++){
for (int j = 0; j < array.length - 1 - i; j++) {
if(array[j] > array[j + 1]){
int tmp = array[j];
array[j]=array[j + 1];
array[j + 1] = tmp;
}
}
}
}
public static void main(String[] args) {
int[] array = {1,4,2,5,3,7,6,9,8};
System.out.println("排序前:"+Arrays.toString(array));
bubbleSort(array);
System.out.println("排序后:"+Arrays.toString(array));
}
优化后的代码:
public static void bubbleSort(int[] array){
//i表示趟数
for(int i = 0; i < array.length - 1;i++){
boolean flg = false;
for (int j = 0; j < array.length - 1 - i; j++) {
if(array[j] > array[j + 1]){
int tmp = array[j];
array[j]=array[j + 1];
array[j + 1] = tmp;
flg = true;
}
}
if(flg == false){
break;
}
}
}
public static void main(String[] args) {
int[] array = {1,4,2,5,3,7,6,9,8};
System.out.println("排序前:"+Arrays.toString(array));
bubbleSort(array);
System.out.println("排序后:"+Arrays.toString(array));
}
public static void reverse(int[] array){
int left = 0;
int right = array.length-1;
while (left < right){
int tmp = array[left];
array[left] = array[right];
array[right] = tmp;
left++;
right--;
}
}
public static void main(String[] args) {
int[] array = {1,234,6,5};
System.out.println("逆置前"+Arrays.toString(array));
reverse(array);
System.out.println("逆置后"+Arrays.toString(array));
}