Java.util.Arrays
java.util.Arrays类中提供了许多实用的方法,可用于数组的复制、排序等操作处理。
以下是常用的方法和属性:
- Arrays.sort() 串行排序:
源代码:
public static void sort(int[] a, int fromIndex, int toIndex) {
rangeCheck(a.length, fromIndex, toIndex);
DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
//双轴快速排序比一般的快速排序更快,优化后的快速排序
}
sort(数组名,起始下标,结束下标) //填充指定索引范围,按照升序进行数组排序,如果指定范围,则排序范围从起始索引(包括)到结束索引(不包括)。如果起始索引==结束索引,则排序范围为空。如果指定下标索引越界,则会抛出异常ArrayIndexOutOfBoundsException。这里底层用到rangeCheck()方法即范围检查,rangeCheck()方法具体如何实现如下:
private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
if (fromIndex > toIndex) {
throw new IllegalArgumentException(
"fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
}
if (fromIndex < 0) {
throw new ArrayIndexOutOfBoundsException(fromIndex);
}
if (toIndex > arrayLength) {
throw new ArrayIndexOutOfBoundsException(toIndex);
}
}
- 举例:
package kkee;
import java.util.Arrays;
/**
* @Package: kkee
* @Author:kkz
* @Description:
* @Date:Created in 2018/10/18 20:53
*/
public class Arraysort {
public static void main(String[] args) {
int[] array1 = {1,4,2,5,7,3,8,6};
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));
}
}
[1, 2, 3, 4, 5, 6, 7, 8]
- Arrays.binarySearch() 二分查找:
使用二分查找来搜索指定类型的数组,以查找指定的键值。源代码:
public static int binarySearch(int[] a, int key) {
return binarySearch0(a, 0, a.length, key);
}
public static int binarySearch(int[] a, int fromIndex, int toIndex,
int key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1; //无符号右移 = mid=low+(high-low)/2
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
binarySearch(数组名,键值) / binarySearch(数组名,起始索引,结束索引,键值) //在指定索引范围内搜索,键值即我们要查找的数字,如果搜索键包含在数组中,则返回其索引,即在数组中的下标;否则返回(—(插入点)—1)。插入点为第一个大于键值的元素索引。如果指定下标索引越界,则会抛出异常ArrayIndexOutOfBoundsException。
- Arrays.equals() :
判断两个数组是否相等,结果为true或者false。源代码:
public static boolean equals(int[] a, int[] a2) {
if (a==a2)
return true; //判断数组是否相同;若相同,则结果为true
if (a==null || a2==null)
return false; //判断两个数组是否为空
int length = a.length; //声明数组长度
if (a2.length != length) //判断两个数组长度是否相等
return false;
for (int i=0; i<length; i++)
if (a[i] != a2[i]) //判断数组中元素是否相同
return false;
return true;
如果两个数组以相同顺序包含相同的元素,则两个数组是相等的。注:如果两个数组引用均为null,则认为他们是相等的。
- 举例:
public class Arraysort {
public static void main(String[] args) {
int[] array1 = {1,4,2,5,7,3,8,6};
int[] array2 = {1,2,3,4,5};
int[] array3 = {1,4,2,5,7,3,8,6};
int[] array4 = {1,2,3,4,5,6,7,8};
boolean b1 = Arrays.equals(array1,array2);
boolean b2 = Arrays.equals(array1,array3);
boolean b3 = Arrays.equals(array1,array4);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
}
}
false
true
false
- Arrays.fill():
使用指定值填充数组。源代码:
public static void fill(int[] a, int val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}
public static void fill(int[] a, int fromIndex, int toIndex, int val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}
fill(数组名,值)/fill(数组名,起始索引,结束索引,键值,值)//填充指定索引范围,如果指定范围,则填充从起始索引(包括)到结束索引(不包括)。注:如果起始索引==结束索引,则填充范围为空。如果指定下标索引越界,则会抛出异常ArrayIndexOutOfBoundsException。
- 举例:
public class Arraysort {
public static void main(String[] args) {
int[] array1 = {1,4,2,5,7,3,8,6};
int[] array2 = {1,2,3,4,5};
int[] array3 = {1,4,2,5,7,3,8,6};
int[] array4 = {1,2,3,4,5,6,7,8};
Arrays.fill(array4 , 5); //用一个数填充整个数组,未定义起始索引和结束索引则默认整个
System.out.println(Arrays.toString(array4));
Arrays.fill(array4 , 0 ,5,0); //从array[0]开始到aaray[5]用0填充,剩余不变
System.out.println(Arrays.toString(array4));
Arrays.fill(array4 , 0 ,10,0); //下标索引越界,运行时异常
System.out.println(Arrays.toString(array4));
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10
[5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 5, 5, 5]
at java.util.Arrays.rangeCheck(Arrays.java:120)
at java.util.Arrays.fill(Arrays.java:2903)
at kkee.Arraysort.main(Arraysort.java:21)
- Arrays.toString():
返回指定数组的字符串表示形式。源代码:
public static String toString(int[] a) {
if (a == null) //若数组为空,则返回null
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('['); //append()是往动态字符串数组添加 添加[
for (int i = 0; ; i++) {
b.append(a[i]); //添加数组的元素 按下标输出
if (i == iMax) // 如果i==iMax即数组中元素添加结束
return b.append(']').toString(); //添加]
b.append(", ");
}
}
前面的例子中已经使用过,可以查看结果。
数组拷贝
数组拷贝即为数组复制。首先,可以分为浅拷贝和深拷贝。
浅拷贝是指拷贝对象时仅仅拷贝对象本身(包括对象中的基本变量),而不拷贝对象包含的引用指向的对象。深拷贝不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象。举例来说更加清楚:对象A1中包含对B1的引用,B1中包含对C1的引用。浅拷贝A1得到A2,A2 中依然包含对B1的引用,B1中依然包含对C1的引用。深拷贝则是对浅拷贝的递归,深拷贝A1得到A2,A2中包含对B2(B1的copy)的引用,B2 中包含对C2(C1的copy)的引用。
浅拷贝是按位拷贝对象,它会创建一个新对象,这个对象有着原始对象属性值的一份精确拷贝。如果属性是基本类型,拷贝的就是基本类型的值;如果属性是内存地址(引用类型),拷贝的就是内存地址 ,因此如果其中一个对象改变了这个地址,就会影响到另一个对象。
深拷贝会拷贝所有的属性,并拷贝属性指向的动态分配的内存。当对象和它所引用的对象一起拷贝时即发生深拷贝。深拷贝相比于浅拷贝速度较慢并且花销较大。
目前在Java中数据拷贝提供了如下方式:
- for 循环(浅拷贝)
- 数组名.clone() 克隆拷贝 (浅拷贝)
- System.arraycopy() (浅拷贝)
- Arrays.copyOf() (浅拷贝)
for 循环实现数组拷贝
package kkee;
import java.util.Arrays;
public class Arraysort {
public static void main(String[] args) {
int[] array1 = {1, 4, 2, 5, 7, 3, 8, 6};
int[] array2 = {1, 2, 3, 4, 5};
int[] array3 = {1, 4, 2, 5, 7, 3, 8, 6};
int[] array4 = {1, 2, 3, 4, 5, 6, 7, 8};
int[] barry = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
barry[i] = array1[i];
}
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(barry));
barry[1] = 10;
System.out.println(Arrays.toString(barry));
System.out.println(Arrays.toString(array1));
}
}
[1, 4, 2, 5, 7, 3, 8, 6]
[1, 4, 2, 5, 7, 3, 8, 6]
[1, 10, 2, 5, 7, 3, 8, 6]
[1, 4, 2, 5, 7, 3, 8, 6]
class TestArray {
private int val = 10;
public void setVal(int val) { this.val = val; }
public int getVal() { return this.val; }
}
public class Arraysort {
public static void main(String[] args) {
TestArray[] barry1 = new TestArray[4];
barry1[0] = new TestArray();
barry1[1] = new TestArray();
barry1[2] = new TestArray();
barry1[3] = new TestArray();
TestArray[] barry2 = new TestArray[4];
for (int i = 0; i < barry1.length; i++) {
barry2[i] = barry1[i];
}
for (int i = 0; i < barry1.length; i++) {
System.out.print(barry1[i].getVal() + " ");
}
System.out.println();
for (int i = 0; i < barry2.length; i++) {
System.out.print(barry2[i].getVal() + " ");
}
System.out.println();
barry2[0].setVal(100000);
System.out.println("===============");
for (int i = 0; i < barry1.length; i++) {
System.out.print(barry1[i].getVal() + " ");
}
System.out.println();
for (int i = 0; i < barry2.length; i++) {
System.out.print(barry2[i].getVal() + " ");
}
}
}
10 10 10 10
10 10 10 10
===============
100000 10 10 10
100000 10 10 10
由上可得,for循环是浅拷贝,产生一个新的对象,两个引用指向同一个对象,代码灵活,但效率低,速度相对较慢。
克隆拷贝 Object.clone()
在Java语言中,数组是引用类型。如果有两个数组变量a1和a2,则语句“a2 = a1;”将数组变量a1的引用传递给另一个数组a2;如果数组a2发生变化,则数组a1也发生变化。对于引用类型来说,它是浅拷贝;对简单类型来说,它是深拷贝。
引用类型:
package kkee;
import java.util.Arrays;
public class Arraysort {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7,8,9};
int[] barry = array.clone();
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(barry));
barry[0] = 1000;
System.out.println("=================");
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(barry));
}
}
[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]
[1000, 2, 3, 4, 5, 6, 7, 8, 9]
class TestArray {
private int val = 10;
public void setVal(int val) { this.val = val; }
public int getVal() { return this.val; }
}
public class Arraysort {
public static void main(String[] args) {
TestArray[] t1 = new TestArray[4];
t1[0] = new TestArray();
t1[1] = new TestArray();
t1[2] = new TestArray();
t1[3] = new TestArray();
TestArray[] t2 = t1.clone();//t2[0]
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
System.out.println();
t2[0].setVal(100000);
System.out.println("===============");
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
}
10 10 10 10
10 10 10 10
===============
100000 10 10 10
100000 10 10 10
查看clone()的源代码可知:
protected native Object clone() throws CloneNotSupportedException;
它的关键字是native(本地方法),返回类型是Object,所以赋值时要发生强制转换,并且也是由底层的C/C++语言实现的,还要注意的是,这里修饰符是protected,也就是说,我们创建了一个Object类以后,是不能直接调用这个clone()方法的,因为protected关键字只允许同一个包内的类和它的子类调用,所以我们声明一个object类时,肯定不是同一个包内,所以就不能去调用它。
要调用这个方法,就需要我们写一个类,然后声明实现cloneable接口就好了,不需要去显示地声明继承于object,因为java中的类如果不显示说明父类的话,默认父类就是object。然后我们继承这个方法:
@Override
public Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
这里需要是,为了能够在不同包内去调用这个方法,我们需要把这个权限升级为public。现在我们就可以调用这个类的clone()方法去拷贝我们的类了。总得来说clone()就是浅拷贝。
System.arraycopy()
System.arraycopy(src,srcPos,dest,destPos,length)是System类提供的一个静态方法,可以用它来实现数组之间的赋值。查看源代码:
public static native void arraycopy(Object src, int srcPos, Object dest, int desPos, int length)
//参数含义:(原数组, 原数组的开始位置, 目标数组, 目标数组的开始位置, 拷贝个数)
它是浅拷贝,也就是说对于非基本类型而言,它拷贝的是对象的引用,而不是去新建一个新的对象。通过它的代码我们可以看到,这个方法不是用java语言写的,而是底层用c或者c++实现的,因而速度会比较快。
public class Arraysort {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7,8,9};
int[] brray = new int[array.length];
System.arraycopy(array,0,brray,0,array.length);
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(brray));
brray[0] = 1000;
System.out.println("=================");
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(brray));
}
}
[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]
[1000, 2, 3, 4, 5, 6, 7, 8, 9]
class TestArray {
private int val = 10;
public void setVal(int val) { this.val = val; }
public int getVal() { return this.val; }
}
public class Arraysort {
public static void main(String[] args) {
TestArray[] t1 = new TestArray[4];
t1[0] = new TestArray();
t1[1] = new TestArray();
t1[2] = new TestArray();
t1[3] = new TestArray();
TestArray[] t2 = new TestArray[4];
System.arraycopy(t1,0,t2,0,t1.length);
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
System.out.println();
t2[0].setVal(100000);
System.out.println("===============");
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
}
10 10 10 10
10 10 10 10
===============
100000 10 10 10
100000 10 10 10
Arrays.copyOf()
Arrays.copyOf底层其实也是用的System.arraycopy ,所以效率自然低于System.arraycpoy(),所以它也是浅拷贝。
源码如下:
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked") //源数组,拷贝的个数
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
public class Arraysort {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7,8,9};
int[] brray = Arrays.copyOf(array,array.length);
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(brray));
brray[0] = 1000;
System.out.println("=================");
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(brray));
}
}
[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]
[1000, 2, 3, 4, 5, 6, 7, 8, 9]
Arrays.copyOfRange():
复制数组的指定范围内容。源代码:
public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
copyOfRange(数组名,起始索引,结束索引)从起始索引(包括)到结束索引(不包括)。结果为相同数据类型的数组。
练习:
- {1,2,3,4,5,6}数组将奇数放在偶数前面,大小顺序不要求。
package kkee;
import java.util.Arrays;
/**
* @Package: kkee
* @Author:kkz
* @Description:
* @Date:Created in 2018/10/19 1:16
*/
public class EvenoddChange {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < array.length; i++) {
int temp; //设置临时变量
for (int j = array.length - 1; j > 0; j--) {
//将后面的奇数放在偶数前
if (array[j] % 2 == 1 && array[j - 1] % 2 == 0) {
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
System.out.println(Arrays.toString(array));
}
}
[1, 3, 5, 2, 4, 6]
- 一个数组是有序的,给定一个key:数字 有两个数字的和加起来等于key,
找到这两个数字的下标,例 {1,2,3,4,5,6} 7
public class NewArray {
public static void findIndex(int[] array,int key){
for(int i = 0; i < array.length ; i++){
for(int j = 1; j < array.length; j++){
int sum = 0;
sum = array[i] + array[j];
if(sum == key){
System.out.println("所加和等于key对应数组下标及元素: " + i + ": " + array[i]+ " " + " " + "" + j + ": " + array[j] );
}
}
}
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6};
int key = 7;
findIndex(array, key);
}
}
所加和等于key对应数组下标及元素: 0: 1 5: 6
所加和等于key对应数组下标及元素: 1: 2 4: 5
所加和等于key对应数组下标及元素: 2: 3 3: 4
所加和等于key对应数组下标及元素: 3: 4 2: 3
所加和等于key对应数组下标及元素: 4: 5 1: 2
- 一个整形数组,除了两个数字只出现一次外, 其他数字都是两次,找到这两个数字。{1,3,1,2,3,4}
解题思路:运用异或的方法:
1^1= 0
1^0= 1
0^1= 1
(1)对于出现两次的元素,使用“异或”操作后结果肯定为0,那么我们就可以遍历一遍数组,对所有元素使用异或操作,那么得到的结果就是两个出现一次的元素的异或结果。
(2)因为这两个元素不相等,所以异或的结果肯定不是0,也就是可以再异或的结果中找到1位不为0的位,例如异或结果的最后一位不为0。
(3)这样我们就可以最后一位将原数组元素分为两组,一组该位全为1,另一组该位全为0。
(4)再次遍历原数组,最后一位为0的一起异或,最后一位为1的一起异或,两组异或的结果分别对应着两个结果。
package kkee;
import java.util.Arrays;
/**
* @Package: kkee
* @Author:kkz
* @Description:
* @Date:Created in 2018/10/19 10:12
*/
public class ValueGroup {
public static void main(String[] args) {
int[] array = {1, 3, 1, 2, 3, 4};
int[] value = findNums(array);
System.out.println(Arrays.toString(value));
}
public static int[] findNums(int[] arr) {
if(arr.length < 2)
return arr;//判断数组长度是否满足题目要求,至少有3个数组元素
int[] result = new int[2]; //要返回的结果,即只出现一次的两个数字
int res = arr[0]; //第一次对所有元素进行亦或操作结果
for(int i=1; i<arr.length; i++) {
res ^= arr[i];
}
int bitIndex = 0;
for(int i=0; i<32; i++) { //找出异或结果为1的位。
if((res>>i & 1) == 1) {
bitIndex = i;
break;
}
}
for(int i=0; i<arr.length; i++) { //根据bitIndex为1,将元素分为两组
if((arr[i] >> bitIndex & 1) == 1)
result[0] ^= arr[i]; //对应位为1,亦或得到的结果
else
result[1] ^= arr[i]; //对应位为0,亦或得到的结果
}
return result;
}
}
[2, 4]
- 如何排序数组并插入某个元素?
package kkee;
import java.util.Arrays;
import java.util.Scanner;
public class NewArray {
public static void main(String[] args) {
int[] array = {9, 2, 4, 5};
Arrays.sort(array);
System.out.println(Arrays.toString(array));
Scanner input = new Scanner(System.in);
int[] barry = new int[array.length + 1]; //声明并初始化一个新的数组,插入一个数,长度加1
for (int i = 0;i < array.length;i ++){
barry[i] = array[i];
} //将原数组的元素赋给新数组
System.out.print("插入新的元素:");
int value = input.nextInt();
int index = array.length; //插入的数组在新数组的下标
barry[index] = value;
Arrays.sort(barry);
System.out.println("插入后的数组:"+Arrays.toString(barry));
[2, 4, 5, 9]
插入新的元素:8
插入后的数组:[2, 4, 5, 8, 9]
- 如何搜索数组中的最小值和最大元素?
package kkee;
public class NewArray {
public static void main(String[] args) {
int[] array = {9, 2, 4, 5};
// 查找最大元素
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max)
max = array[i];
}
//查找最小元素
int min = array[0];
for (int i = 0;i < array.length; i++){
if(array[i] < min)
min = array[i];
}
System.out.println("Max is " + max);
System.out.println("Min is " + min);
}
}
Max is 9
Min is 2
- 如何合并两个数组(合并到一个新的数组)?
package kkee;
import java.util.Arrays;
public class NewArray {
public static void main(String[] args) {
int[] array = {9, 2, 4, 5};
int[] carry = {2, 3, 4, 5, 6, 7};
int[] darry = new int[10];
System.arraycopy(array, 0, darry, 0,array.length);
System.arraycopy(carry, 0, darry, array.length,carry.length);
System.out.println("darry =" + Arrays.toString(darry));
}
}
darry =[9, 2, 4, 5, 2, 3, 4, 5, 6, 7]
- 如何填充数组(一次填充,部分填充)?
package kkee;
import java.util.Arrays;
public class NewArray {
public static void main(String[] args) {
int[] array = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5};
Arrays.fill(array,1,3,1); //起始索引包括,结束索引不包括 部分填充
System.out.println(Arrays.toString(array));
Arrays.fill(array, 2); //一次填充
System.out.println(Arrays.toString(array));
Arrays.fill(array, 0, 5, 3);
System.out.println(Arrays.toString(array));
}
}
[1, 1, 1, 2, 3, 3, 4, 4, 5, 5]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[3, 3, 3, 3, 3, 2, 2, 2, 2, 2]
- 如何删除数组指定元素?
package kkee;
import java.util.Arrays;
import java.util.Scanner;
public class NewArray {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int[] array = new int[]{1,2,4,5,9,8,0};
System.out.println(Arrays.toString(array));
System.out.println("输入要删除第几个元素:");
int n = sc.nextInt();
array[n-1] = array[array.length-1];//把最后一个元素替代指定删除的元素
array = Arrays.copyOf(array, array.length-1);//数组长度减一,再赋给原来的数组
System.out.println(Arrays.toString(array));
}
}
[1, 2, 4, 5, 9, 8, 0]
输入要删除第几个元素:
3
[1, 2, 0, 5, 9, 8]
- 如何从数组中查找常见的元素?数组有的可以查并返回下标,没有的返回-1。
public class OrderArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 11, 15, 17};
System.out.println("所查找元素在数组中的下标为:" + binarySearch(arr, 15, 0, 10));
System.out.println("所查找元素在数组中的下标为:" + binarySearch(arr, 25, 0, 10));
}
public static int binarySearch(int[] arr, int key,int start,int end) {
start = 0;
end = arr.length - 1;
while (start <= end) {
int middle = start + (end - start) / 2;
if (arr[middle] == key) {
return middle;
} else if (arr[middle] < key) {
start = middle + 1;
} else {
end = middle - 1;
}
}
return -1;
}
}
所查找元素在数组中的下标为:9
所查找元素在数组中的下标为:-1