Home  >  Article  >  Java  >  Commonly used operations on arrays

Commonly used operations on arrays

PHP中文网
PHP中文网Original
2017-06-21 16:40:001435browse

Commonly used operations on arrays

1. Find the maximum and minimum values ​​in the array

Idea: Assumption The element with subscript 0 is the maximum value. Traverse the array and compare it with max in turn. If there is an element larger than max, assign this value to max. The minimum value is the same

 1 public class TestArray{ 2     public static void main(String[] args){ 3         int[] arr={23,45,234,576,34,87,34,12,67}; 4         int max=arr[0]; 5         int min=arr[0]; 6         for(int i=0;i<arr.length;i++){ 7             if(arr[i]>max){ 8                 max=arr[i]; 9             }10             if(arr[i]<min){11                 min=arr[i];12             }13         }14         System.out.println("数组中最大值为:"+max);15         System.out.println("数组中最小值为:"+min);16     }17 }

2. Find whether an element exists in the array

 1 import java.util.Scanner; 2 public class TestArray{ 3     public static void main(String[] args){ 4         Scanner in=new Scanner(System.in); 5         int[] arr={23,45,234,576,34,87,34,12,67}; 6         System.out.println("请输入你要查找的元素"); 7         int element=in.nextInt(); 8         int i,flag=0; 9         for(i=0;i<arr.length;i++){10             if(arr[i]==element){11                 flag=1;12                 break;13             }14         }15         if(flag==1){16             System.out.println("你要查找的元素的下标为:"+i);17         }else{18             System.out.println("你要查找的元素不存在");19         }20     }21 }

(2) Use binary search method to find whether an element exists in the array

Premise: The array to be searched must be ordered (size in order)

Principle: Compare the element to be found with the element in the middle subscript of the array. If it is greater than the middle element, search to the right. If it is smaller than the middle element, search to the left.

 1 public static int binarySearch(int[] arr,int ele){ 2         int left=0; 3         int right=arr.length-1; 4         int mid; 5         int index=-1; 6         while(left<=right){ 7             mid=(left+right)/2; 8             if(arr[mid]==ele){ 9                 index=mid;10                 break;11             }else if(arr[mid]<ele){12                 left=mid+1;13             }else if(arr[mid]>ele){14                 right=mid-1;15             }16         }17         return index;18     }

3. Sort the array

(1), Bubble sort

Principle: Compare adjacent elements, small ones go forward, big ones go backward, and the maximum index where the maximum value appears

Analysis: When comparing for the first time, the larger one will appear later, and the maximum value will be ranked at the largest index

. When comparing for the second time, because the maximum value has been determined, you only need to compare the previous n-1 elements are sufficient. Determine the second largest value and rank it at the second largest index

. Determine the third largest value and the fourth largest value in turn... .......

Conclusion: N numbers are queued up, and the smaller ones are placed first. The outer loop is n-1, and the inner loop is n-1-i

 1 public class TestArray{ 2     public static void main(String[] args){ 3         int[] arr={10,3,8,1,6}; 4         //外层循环控制比较轮数 5         for(int i=0;i<arr.length-1;i++){ 6             //内层循环控制每轮比较次数 7             for(int j=0;j<arr.length-1-i;j++){ 8                 if(arr[j]>arr[j+1]){ 9                     int temp=arr[j];10                     arr[j]=arr[j+1];11                     arr[j+1]=temp;12                 }13             }14         }15         //遍历数组16         for(int i=0;i<arr.length;i++){17             System.out.println(arr[i]);18         }19     }20 }

(2), selection sorting

Principle: Starting from subscript 0, compare it with the following elements in sequence. If the latter element is smaller than the element with subscript 0, transpose it. Compare the new element with index 0 to the following element. The first time is completed, the minimum value appears at index 0

Example: {10, 3, 8, 1, 6}

The first round of comparison starts from the 0 subscript element, in sequence To compare with the following elements, first compare 10 and 3, 10<3, swap positions, the element with subscript 0 becomes 3, {3, 10, 8, 1, 6}; then compare 3 and 8, 3< ;8, no transposition; compare 3 and 1, 3>1, transpose

{1, 10, 8, 3, 6}, then compare 1 and 6, 1<6, no Change position. The first round ends, {1, 10, 8, 3, 6}

The second round of comparison, the previous round has determined that the element with subscript 0 is the minimum value, this round of comparison starts from subscript 1 , first compare 10 and 8, transpose {1, 8, 10, 3, 6}; compare 8 and 3, transpose {1, 3, 10, 8, 6}, compare 3 and 6, do not transpose. At the end of the second round, it is determined that the penultimate element is at subscript 1.

..........

Compare length-1 round in total.

 1 public class TestArray{ 2     public static void main(String[] args){ 3         int[] arr={10,3,8,1,6}; 4         for(int i=0;i<arr.length-1;i++){ 5             for(int j=i+1;j<arr.length;j++){ 6                 if(arr[i]>arr[j]){ 7                     int temp=arr[i]; 8                     arr[i]=arr[j]; 9                     arr[j]=temp;10                 }11             }12         }13         //遍历数组14         for(int i=0;i<arr.length;i++){15             System.out.println(arr[i]);16         }17     }18 }

4. Delete the elements in the array

( 1) Delete elements according to the subscript (fill the gaps with 0)

1 public static void delete(int[] arr,int index){2         for(int i=index;i<arr.length-1;i++){3             arr[i]=arr[i+1];4         }5         arr[arr.length-1]=0;6         System.out.println(Arrays.toString(arr));7     }

(2) Delete the corresponding elements in the array according to the input elements

 1 public static void delete(int[] arr,int ele){ 2         int index=-1; 3         for(int i=0;i<arr.length;i++){ 4             if(arr[i]==ele){ 5                 index=i; 6             } 7         } 8         for(int i=index;i<arr.length-1;i++){ 9             arr[i]=arr[i+1];10         }11         arr[arr.length-1]=0;12         System.out.println(Arrays.toString(arr));13     }

下面介绍一些API里面常见的对数组的操作

在java中,除java.lang包下的类和接口可以直接使用外,其他包下的类或接口在使用时需要先导包。
java.util.Arrays类:此类包含用来操作数组(比如排序和搜索)的各种方法。

这些都是静态方法,可以类名.方法名直接使用,这里都以int型数组为例

1、对数组进行快速排序

Arrays.sort(int[] arr);对传入的数组默认进行升序排序

2、返回指定数组内容的字符串表现形式。

Arrays.toString(int[] arr);

3、使用二分法搜索制定数组中的某个元素的下标

Arrays.binarySearch(int[] arr);

4、将将指定的 int 值分配给指定 int 型数组的每个元素。

Arrays.fill(int[] arr,int val);

5、复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。

Arrays.copyOf(int[] arr,int newLength);它的返回值是一个数组

6、将指定数组的指定范围复制到一个新数组。 包含起始位置但不包含结束位置。

Arrays.copyOfRange(int[] arr,int from,int to);它的返回值是一个数组

其他数组知识:

1、命令行参数:可以在执行java命令时为main方法传入参数值。

用法:运行java命令时传入命令行参数: java 类名 "值1" "值2"...

public static void main(String[] args){},我们可以看到main方法是一个有参的方法,参数是一个字符串数组,在命令行为main方法传值时,传入的值都保存在args字符数组里。
注意:多个参数值之间用空格分割。参数的值将会保存到字符串数组传入main方法,下标从零开始。
在获取命令行参数时需要注意下标不能越界,最大下标应该为参数的个数-1

public static void main(String[] args){2         for(int i=0;i<args.length;i++){3                 System.out.println(args[i]);4         }5 }

2、可变参数

可变参数是java1.5之后的新特性,可以代表零到多个相同数据类型的变量,是为了解决因参数个数的变化而导致过多的方法重载问题。

注意:1、可变参数只能用于形式参数(方法定义时),可以把可变参数当作数组来处理。

   2、一个方法在最多只能有一个可变参数,可变参数必须作为最后一个参数。

   3、调用带可变参数的方法时,数据类型必须与可变参数的类型对应。

 1 public class Test1 { 2     public static void main(String[] args){ 3         double sum=add(4,2.1,3.4,1.2); 4         System.out.println(sum); 5     } 6     public static double add(int a,double...b){ 7         double sum=a; 8         for(int i=0;i<b.length;i++){ 9             sum+=b[i];10         }11         return sum;12     }13 }

例题:

  1. 合并数组操作:现有如下一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: int newArr [] ={1,3,4,5,6,6,5,4,7,6,7,5}

  思路: 确定出不为0的个数,这样可以开辟新数组;从旧的数组之中,取出内容,并将其赋给新开辟的数组。

 1 public class Test1 { 2     public static void main(String[] args){ 3         int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; 4         int[] arr=mergeArrays(oldArr); 5         System.out.println(Arrays.toString(arr)); 6     } 7     public static int[] mergeArrays(int[] oldArr){ 8         int count=0; 9         for(int i=0;i<oldArr.length;i++){10             if(oldArr[i]!=0){11                 count++;12             }13         }14         int[] newArr=new int[count];15         int index=0;16         for(int i=0;i<oldArr.length;i++){17             if(oldArr[i]!=0){18                 newArr[index]=oldArr[i];19                 index++;20             }21         }22         return newArr;23     }24 }

  2、使用二分法查找有序数组中元素。找到返回索引,不存在输出-1。使用递归实现

 1 public class Test1 { 2     public static void main(String[] args){ 3         int[] arr={1,2,3,4,5,6,7,8}; 4         int index=binarySearch(arr,6,0,arr.length-1); 5         System.out.println(index); 6     } 7     public static int binarySearch(int[] arr,int ele,int left,int right){ 8         int mid=(left+right)/2; 9         if(arr[mid]==ele){10             return mid;11         }else if(arr[mid]<ele){12             return binarySearch(arr,ele,mid+1,right);13         }else if(arr[mid]>ele){14             return binarySearch(arr,ele,left,mid-1);15         }16         return -1;17     }18 }</p>

 

 

 

 

The above is the detailed content of Commonly used operations on arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn