java中数组传递

本文通过三个实例:整型数组、字符串数组及对象数组,详细解释了如何在Java中实现数组值的交换,并说明了虽然数组地址不变,但可以通过方法调用成功交换数组中的元素值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整型数组的情形:

public class TestArrayINT {

 static void changeT (int[] a, int[] b) {

      int temp;

     for(int i = 0 ; i < a.length ; i ++){

           temp = a[i];

           a[i] = b[i];

           b[i] = temp;

     }

 }

   public static void main(String[] args) {

      int A[] = {1,2,3,4,5};

      int B[] = {0,0,0,0,0};

      System.out.println(A);

      System.out.println(B);

      for(int i = 0 ; i <A.length ; i ++){

         System.out.print(A[i] + " ");

      }

      System.out.println();

       for(int i = 0 ; i <B.length ; i ++){

         System.out.print(B[i] + " ");

      }

       System.out.println();

       changeT(A,B);

      for(int i = 0 ; i <A.length ; i ++){

         System.out.print(A[i] + " ");

      }

      System.out.println();

      for(int i = 0 ; i <B.length ; i ++){

         System.out.print(B[i] + " ");

      }

      System.out.println(A);

      System.out.println(B);

   }

}   

   从结果看,AB的地址并没有改变,但是却实现了数组A与数组B的值的改变;

字符串数组的情形:

public class TestArrayString {

 static void changeT (String[] a, String[] b) {

      String temp;

     for(int i = 0 ; i < a.length ; i ++){

           temp = a[i];

           a[i] = b[i];

           b[i] = temp;

     }

 }

   public static void main(String[] args) {

      String A[] = {"1","2","3","4","5"};

      String B[] = {"0","0","0","0","0"};

      System.out.println(A);

      System.out.println(B);

      for(int i = 0 ; i <A.length ; i ++){

         System.out.print(A[i] + " ");

      }

      System.out.println();

       for(int i = 0 ; i <B.length ; i ++){

         System.out.print(B[i] + " ");

      }

       System.out.println();

      changeT(A,B);

      for(int i = 0 ; i <A.length ; i ++){

         System.out.print(A[i] + " ");

      }

      System.out.println();

      for(int i = 0 ; i <B.length ; i ++){

         System.out.print(B[i] + " ");

      }

      System.out.println();

      System.out.println(A);

      System.out.println(B);

   }   }

 

  从结果看,AB的地址并没有改变,但是却实现了数组A与数组B的值的改变;

对象数组的情形:

class person {

   private String name;

   public person(String name){

      this.name = name;

   }

   public String getName() {

      return name;

   } 

}

public class TestObjuectString { 

 static void changeT (person[] a, person[] b) {

     person temp;

     for(int i = 0 ; i < a.length ; i ++){

           temp = a[i];

           a[i] = b[i];

           b[i] = temp;

     }

 }

   public static void main(String[] args) {

      person A[] = {new person("tom"),new person("jack"),new person("lucy"),new person("tim")};

      person B[] = {new person("o1"),new person("o2"),new person("o3"),new person("o4")};

      System.out.println(A);

      System.out.println(B);

      for(int i = 0 ; i <A.length ; i ++){

         System.out.print(A[i].getName() + " ");

      }

      System.out.println();

       for(int i = 0 ; i <B.length ; i ++){

         System.out.print(B[i].getName() + " ");

      }

       System.out.println();

      changeT(A,B);

      for(int i = 0 ; i <A.length ; i ++){

         System.out.print(A[i].getName() + " ");

      }

      System.out.println();

      for(int i = 0 ; i <B.length ; i ++){

         System.out.print(B[i].getName() + " ");

      }

      System.out.println();

      System.out.println(A);

      System.out.println(B);

   }

}

 

从结果看,AB的地址并没有改变,但是却实现了数组A与数组B的值的改变;

 

    由此可以的得到:在数组传递的过程中是可以改变数组中各个元素的值,但是数组的地址是不变的;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值