例:数组的倒置以及合并
public class TestDemo1 {
//反转数组
public static void reverse(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 merge(int[] array1,int[] array2){
int[] array3 = new int[array1.length+array2.length];
for(int i = 0;i < array1.length;i++){
array3[i] = array1[i];
}
for(int j = 0;j < array2.length;j++){
array3[array1.length+j] = array2[j];
}
for(int k = 0;k < array3.length;k++){
System.out.print(array3[k]+" ");
}
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {12,34,21,22,4,6,81,2,1};
int[] array2 = {5,8,10};
reverse(array);
merge(array,array2);
}
}
运行结果如下:
二分查找例题
public class TestDemo1 {
public static int Search(int[] array,int val){
int low = 0;
int high = array.length-1;
while(low<=high){
//int mid = (low+high)/2;
//int mid = low+(high-low)/2;
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) 查找数据的插入点位置
}
public static void main(String[] args) {
int[] array = {12,34,21,22,4,6,81,2,1};
Arrays.sort(array);
System.out.println(Arrays.toString(array));
int index = Arrays.binarySearch(array,12);
System.out.println(index);
//binarySearch 二分查找
}
}
运行结果如下:
二维数组的遍历
基本类型
public static void show1(int[][] array){
for(int i = 0;i<array.length;i++){
for(int j = 0;j<array[i].length;j++){
System.out.println(array[i][j]+" ");
}
System.out.println();
}
}
引用类型
public static void show2(TestArray[][] Array)
{
for (int i = 0; i < Array.length; i++) {
for (int j = 0; j < Array[i].length; j++) {
System.out.print(Array[i][j].getA()+" ");
}
System.out.println();
}
}
二维数组的创建
public class TestDemo2 {
public static void show1(int[][] array){
for(int i = 0;i<array.length;i++){
for(int j = 0;j<array[i].length;j++){
System.out.println(array[i][j]+" ");
}
System.out.println();
}
}
public static void show2(int[][] array){
for(int[] brr: array){
for(int i:brr){
System.out.print(i+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] array = new int[2][3];
array[0][0] = 100;
int[][] array2 = {{1,2,3},{4,5,6}};
int[][] array3 = new int[][]{{1,2,3},{4,5,6}};
show2(array);
System.out.println("========");
show2(array2);
System.out.println("========");
show2(array3);
System.out.println("========");
//不规则数组
int[][] array4 = new int[2][];
array4[0] = new int[3];
array4[1] = new int[2];
array4[0][0] = 100;
show2(array4);
//System.out.println(array4[0][0]);
//int[][] array5 = new int[][3]; //格式错误
}
}
拷贝方式
(1)for循环
基本类型
public class TestDemo3 {
public static void show1(int[][] array){
for(int i = 0;i<array.length;i++){
for(int j = 0;j<array[i].length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] array = {{1,2,3},{4,5,6}};
int[][] array2 = new int[2][3];
//for循环拷贝
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array2[i][j] = array[i][j];
}
}
show1(array);
System.out.println("========");
show1(array2);
array2[0][0] = 100;
System.out.println("====改变后====");
show1(array);
System.out.println("========");
show1(array2);
}
}
引用类型
class TestArray{
private int a=10;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
public class TestDemo3 {
public static void show2(TestArray[][] Array)
{
for (int i = 0; i < Array.length; i++) {
for (int j = 0; j < Array[i].length; j++) {
System.out.print(Array[i][j].getA()+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
TestArray[][] a = new TestArray[2][3];
TestArray[][] b = new TestArray[2][3];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = new TestArray();
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
b[i][j] = a[i][j];
}
}
show2(a);
System.out.println("========");
show2(b);
b[0][0].setA(1000);
System.out.println("====改变后====");
show2(a);
System.out.println("========");
show2(b);
}
}
(2)clone方法
基本类型
public class TestDemo4 {
public static void show1(int[][] array){
for(int i = 0;i<array.length;i++){
for(int j = 0;j<array[i].length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = {{1,2,3},{4,5,6}};
int[][] b = new int[2][3] ;
for (int i = 0; i < a.length; i++) {
b[i] = a[i].clone();
}
show1(a);
System.out.println("========");
show1(b);
b[0][0] = 100;
System.out.println("====改变后====");
show1(a);
System.out.println("========");
show1(b);
System.out.println("========");
int[] array3 = {1,2,3,4,5};
int[] array4;
System.out.println(array3);
array4 = array3.clone();
System.out.println(array4);
}
}
引用类型
class TestArray2{
private int a=10;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
public class TestDemo4 {
public static void show2(TestArray[][] Array)
{
for (int i = 0; i < Array.length; i++) {
for (int j = 0; j < Array[i].length; j++) {
System.out.print(Array[i][j].getA()+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestArray[][] a = new TestArray[2][3];
TestArray[][] b = new TestArray[2][3];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] =new TestArray();
}
}
for (int j = 0; j < a.length; j++) {
b[j] = a[j].clone();
}
show2(a);
System.out.println("========");
show2(b);
b[0][0].setA(1000);
System.out.println("====改变后====");
show2(a);
System.out.println("========");
show2(b);
}
}
(3)System.arraycopy拷贝
基本类型
public class TestDemo6 {
public static void show1(int[][] array){
for(int i = 0;i<array.length;i++){
for(int j = 0;j<array[i].length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] array = {{1,2,3},{4,5,6}};
int[][] array2 = new int[2][3];
for(int i = 0 ;i<array.length;i++){
System.arraycopy(array[i], 0, array2[i], 0, array[i].length);
}
show1(array);
System.out.println("======");
show1(array2);
array2[0][0] = 1000;
System.out.println("====改变后====");
show1(array);
System.out.println("======");
show1(array2);
}
}
引用类型
class TestArray3{
private int a=10;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
public class TestDemo6 {
public static void show2(TestArray3[][] Array)
{
for (int i = 0; i < Array.length; i++) {
for (int j = 0; j < Array[i].length; j++) {
System.out.print(Array[i][j].getA()+" ");
}
System.out.println();
}
}
public static void main1(String[] args) {
TestArray3[][] t1 = new TestArray3[2][3];
TestArray3[][] t2 = new TestArray3[2][3];
for (int i = 0; i < t1.length; i++) {
for (int j = 0; j < t1[i].length; j++) {
t1[i][j] =new TestArray3();
}
}
for(int i = 0 ;i<t1.length;i++){
System.arraycopy(t1[i], 0, t2[i], 0, t1[i].length);
}
show2(t1);
System.out.println("=======");
show2(t2);
t2[0][0].setA(1000);
System.out.println("====改变后====");
show2(t1);
System.out.println("=======");
show2(t2);
}
}
(4)Arrays.copyof方法拷贝
基本类型
public class TestDemo7 {
public static void show1(int[][] array){
for(int i = 0;i<array.length;i++){
for(int j = 0;j<array[i].length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] array = {{1,2,3},{4,5,6}};
int[][] array2 = new int[2][3];
array2 = Arrays.copyOf(array,array2.length);
show1(array);
System.out.println("=========");
show1(array2);
array2[0][0] = 1000;
System.out.println("=====改变后哦====");
show1(array);
System.out.println("=========");
show1(array2);
}
}
引用类型
class TestArray4{
private int a=10;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
public class TestDemo7 {
public static void show2(TestArray4[][] Array)
{
for (int i = 0; i < Array.length; i++) {
for (int j = 0; j < Array[i].length; j++) {
System.out.print(Array[i][j].getA()+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
TestArray4[][] t1 = new TestArray4[2][3];
TestArray4[][] t2 = new TestArray4[2][3];
for (int i = 0; i < t1.length; i++) {
for (int j = 0; j < t1[i].length; j++) {
t1[i][j] =new TestArray4();
}
}
t2 = Arrays.copyOf(t1, t1.length);
show2(t1);
System.out.println("=========");
show2(t2);
System.out.println("=====改变后哦====");
t2[0][0].setA(100);
show2(t1);
System.out.println("=========");
show2(t2);
}
}