# java 数组的相关题

1. 如何搜索数组中的最小值和最大元素？
package Practice;

public class Practice1 {
public static int min(int[] array){
int min=array[0];
for(int i=0;i<array.length;i++){
if(min>array[i]){
min=array[i];
}
}
System.out.println("Min:"+min);
return min;
}

public static int max(int[] array){
int max=array[0];
for(int j=0;j<array.length;j++){
if(max<array[j]){
max=array[j];
}
}
System.out.println("Max:"+max);
return max;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array1={1,4,65,78,9,32,21,6};
min(array1);
max(array1);
}

}


2.如何删除数组中指定元素？

package Practice;

import java.util.Scanner;

public class Practice2 {
public static int index(int i,int[] array){

for(int j=0;j<array.length-1;j++){
if(i<=j){
array[j]=array[j+1];
}
}
array[array.length-1]=0;
return i;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array2={1,2,3,4,5,6};
Scanner in=new Scanner(System.in);
int a= in.nextInt();
index(a, array2);
for(int i=0;i<array2.length;i++){
System.out.print(array2[i]+" ");
}
System.out.println();
}
}


3、 如何从数组中查找常见的元素？

package Practice;

import java.util.Arrays;

public class Practice3 {

public static int search(int[] array,int val){
int low=0;
int high=array.length-1;
while(low<=high){
int mid=(low+high)>>>1;//下标
if(array[mid]<val){
high=mid-1;
}else if(array[mid]<val){
low=mid+1;
}else{
return mid;
}
}
return -(low+1);//low 查找的数据的插入点位置  -1 0
//大
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array={5,76,9,4,21,56,8};
Arrays.sort(array);//记到心里
System.out.println("========");

System.out.println(Arrays.toString(array));
int index=Arrays.binarySearch(array,56);
System.out.println(index);
//Arrays.binarySearch  二分查找
System.out.println("========");
System.out.println(Arrays.toString(array));
}

}

4、如何填充数组？

package Practice;

import java.util.Arrays;

public class practice4 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = new int[]{1,4,6,8,3};
//  填充数组，从第一个元素到接下来的3-1个元素
Arrays.fill(array,6);//一次填充
for(int e:array){
System.out.print(e+",");
}
System.out.println();
Arrays.fill(array,1,3,9);//部分填充
for(int e:array){
System.out.print(e+",");
}
}
}


5、如何排序和比较数组？

package Practice;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Practice5 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array1={35,22,16,7,29,31};
int[] array2=new int[array1.length];
int[] array3={3,32,12,56,67,32};
array2=array1.clone();
Arrays.sort(array1);
Arrays.sort(array2);
Arrays.sort(array3);

if (Arrays.equals(array1, array2)) {
System.out.println("两个数组中的元素值相同");
} else {
System.out.println("两个数组中的元素值不相同");
}
if (Arrays.equals(array1, array3)) {
System.out.println("两个数组中的元素值相同");
} else {
System.out.println("两个数组中的元素值不相同");
}
}
}


6、如何排序数组并插入某个元素？

package JiaYou;

import java.util.Arrays;

public class Day7 {
public static int[] Insert(int[] array,int val,int index){
int[] array2=new int[array.length+1];
System.arraycopy(array, 0, array2, 0, index);
array2[index]=val;
System.arraycopy(array, index, array2, index+1, array.length-index);
return array2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array={11,2,32,14,5};
Arrays.sort(array);
//System.out.println(Arrays.toString(array));
int[] array2=Insert(array,22,3);
System.out.println(Arrays.toString(array2));
}

}


7、如何合并两个数组？

package JiaYou;

import java.util.Arrays;

public class Day73 {
public static int[] merge(int[] array,int[] array2){
int[] array3=new int[array.length+array2.length];
for(int i=0;i<array.length;i++){
array3[i]=array[i];
}
for(int i=0;i<array2.length;i++){
array3[array.length+i]=array2[i];
}
System.out.println(Arrays.toString(array3));
return array3;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array={11,2,32,14,5};
int[] array2={7,6,5,4,3};
merge(array,array2);

}

}


8、初始化后如何扩展数组？

package Practice;

import java.util.Arrays;

public class Practice6 {
public static void ectArray(){
String[] str={"A","B","C","D"};
String[] str2=new String[str.length+2];
str2[str.length]="E";
str2[str.length+1]="F";
System.arraycopy(str,0,str2,0,str.length);
System.out.println(Arrays.toString(str2));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ectArray();
}

}


9、如何排序数组并搜索某个元素，找到返回下标？

package Practice;

import java.util.Arrays;

public class Practice7 {
public static int search(int[] array,int val){
int low=0;
int high=array.length-1;
while(low<=high){
int mid=(low+high)>>>1;//下标
if(array[mid]>val){
high=mid-1;
}else if(array[mid]<val){
low=mid-1;
}else{
return mid;
}
}
return -(low+1);//low 查找的数据的插入点位置
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array={12,34,21,22,4,6,81,2,1};
Arrays.sort(array);
System.out.println(Arrays.toString(array));
int index=search(array,22);
System.out.println(index);
}

}


10、如何反转数组？

package Practice;

import java.util.Arrays;

public class Practice8 {
public static void  rerves(int[] array){
for(int i=0;i<array.length/2;i++){
int tmp=array[i];
array[i]=array[array.length-1-i];
array[array.length-1-i]=tmp;
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array={12,34,21,22,4,6,81,2,1};
rerves(array);
System.out.println(Arrays.toString(array));
}

}


11、如何将字符串数组写入输出控制台？（打印一个String数组）？

package JiaYou;

public class Day71 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String[] string=new String[3];
string[0]="I";
string[1]="love";
string[2]="China!";
for(int i=0;i<string.length;i++){
System.out.print(string[i]+" ");
}
}

}


12、如何确定二维数组的上限？

package Practice;

public class Practice9 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] array={{1,2,3},{2,3,4,5,6}};
System.out.println(array.length);
System.out.println(array[0].length);
System.out.println(array[1].length);
}

}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120