Last active
September 10, 2022 03:36
-
-
Save XDo0/e5f6a74f60e8a4c3b055cc2039f9c9ab to your computer and use it in GitHub Desktop.
Java Array Operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// extend | |
System.arraycopy(names, 0, extended, 0, names.length); | |
// difference | |
objArray.removeAll(objArray2); | |
// intersection | |
objArray.retainAll(objArray2); | |
// union | |
Set<String> set = new HashSet<String>(); | |
... | |
return set.toArray(result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
// Arrays.fill(arrayname,value) | |
// Arrays.fill(arrayname ,starting index ,ending index ,value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String a[] = { "A", "E", "I" }; | |
String b[] = { "O", "U" }; | |
List list = new ArrayList(Arrays.asList(a)); | |
list.addAll(Arrays.asList(b)); | |
Object[] c = list.toArray(); | |
//other method | |
import java.nio.*; | |
public class ConcatArrays { | |
public static void main(String[] args) { | |
int[] myNumbers=new int[]{1,2,3,4}; | |
int[] yourNumbers=new int[]{5,6,7}; | |
int[] theirNumbers=new int[]{8,9,0}; | |
//按需要分配buffer | |
IntBuffer intBuffer = IntBuffer.allocate(myNumbers.length+yourNumbers.length+theirNumbers.length); | |
//放入到buffer | |
intBuffer.put(myNumbers); | |
intBuffer.put(yourNumbers); | |
intBuffer.put(theirNumbers); | |
//得到合并后的数组 | |
int[] allNumber = intBuffer.array(); | |
//显示合并后的数组 | |
for (int i :allNumber ) { | |
System.out.print(i); | |
System.out.print(", "); | |
} | |
//各种数据原始类型都有buffer可用,非常方便 | |
LongBuffer longBuffer = LongBuffer.allocate(10); | |
DoubleBuffer doubleBuffer = DoubleBuffer.allocate(10); | |
FloatBuffer floatBuffer = FloatBuffer.allocate(10); | |
ByteBuffer byteBuffer = ByteBuffer.allocate(10); | |
ShortBuffer shortBuffer = ShortBuffer.allocate(10); | |
CharBuffer charBuffer = CharBuffer.allocate(10); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void reverse(Integer a[]) | |
{ | |
Collections.reverse(Arrays.asList(a)); | |
System.out.println(Arrays.asList(a)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// contain | |
ArrayList<String> objArray = new ArrayList<String>(); | |
objArray.contains("common2"); | |
objArray2.contains(objArray);// always false, because return index of objArray, i.e.,-1 | |
//equal | |
Arrays.equals(ary, ary1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static int[] insertElement(int original[], | |
int element, int index) { | |
int length = original.length; | |
int destination[] = new int[length + 1]; | |
System.arraycopy(original, 0, destination, 0, index); | |
destination[index] = element; | |
System.arraycopy(original, index, destination, index | |
+ 1, length - index); | |
return destination; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 }; | |
int fromindex=1,toindex=4; | |
Arrays.sort(array); | |
//sort from fromindex to toindex-1 | |
Arrays.sort(array, fromIndex,toIndex); | |
//descending | |
Arrays.sort(array, Collections.reverseOrder()); | |
int index = Arrays.binarySearch(array, 2);//2 is the value | |
int len = array.length; | |
int min = (int) Collections.min(Arrays.asList(array)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment