优惠网站代理怎么做,免费的会员卡管理软件,消除wordpress,安徽网站开发培训前言
这个主要就是想记录一个点#xff0c;就是二维数组保存的元素就是一维数组的地址#xff0c;这个概念大家都知道了#xff0c;那么接下来就是我最近写程序发生的一个事情了。
随机打乱一个一维数组
这个程序我相信大家都是会写的#xff0c;通过randomArr来随机打乱…前言
这个主要就是想记录一个点就是二维数组保存的元素就是一维数组的地址这个概念大家都知道了那么接下来就是我最近写程序发生的一个事情了。
随机打乱一个一维数组
这个程序我相信大家都是会写的通过randomArr来随机打乱整个数组之后通过printArr输出即可那如果把调用循环来打乱数组并且把打乱的数组储存在二维数组中又会发生什么情况呢。
import java.util.Random;public class Test {public static void main(String[] args) {int[] arr {1, 2, 3, 4, 5, 6, 7, 8, 9};printArr(randomArr(arr));}//打乱整个数组public static int[] randomArr(int[] arr) {Random r new Random();for (int i 0; i arr.length; i) {//随机生成两个坐标int pos1 r.nextInt(arr.length);int pos2 r.nextInt(arr.length);int tmp arr[pos1];arr[pos1] arr[pos2];arr[pos2] tmp;}return arr;}//输出整个数组public static void printArr(int[] arr) {System.out.print([);for (int i 0; i arr.length; i) {if (i ! arr.length - 1) {System.out.print(arr[i] ,);} else {System.out.print(arr[i]);}}System.out.println(]);}
}随机打乱二维数组里面的一维数组
import java.util.Random;public class Test {public static void main(String[] args) {int[] arr {1, 2, 3, 4, 5, 6, 7, 8, 9};int[][] res new int[5][arr.length];for (int i 0; i res.length; i) {//后面打乱依赖前面打乱生成的res[i] randomArr(arr);printArr(randomArr(arr));}}//打乱整个数组public static int[] randomArr(int[] arr) {Random r new Random();for (int i 0; i arr.length; i) {//随机生成两个坐标int pos1 r.nextInt(arr.length);int pos2 r.nextInt(arr.length);int tmp arr[pos1];arr[pos1] arr[pos2];arr[pos2] tmp;}return arr;}//输出整个数组public static void printArr(int[] arr) {System.out.print([);for (int i 0; i arr.length; i) {if (i ! arr.length - 1) {System.out.print(arr[i] ,);} else {System.out.print(arr[i]);}}System.out.println(]);}
}程序运行结果如下 [5,4,9,6,1,8,3,2,7] [7,1,4,5,2,8,9,3,6] [7,3,2,5,6,1,8,9,4] [7,5,3,1,8,6,2,4,9] [3,9,4,8,7,6,5,1,2] 理论上也确实是这个结果但是此时res数组里面的元素呢
import java.util.Random;public class Test6 {public static void main(String[] args) {int[] arr {1, 2, 3, 4, 5, 6, 7, 8, 9}; int[][] res new int[5][arr.length];for (int i 0; i res.length; i) {//后面打乱依赖前面打乱生成的res[i] randomArr(arr);}//打印resfor (int i 0; i res.length; i) {printArr(res[i]);}}//打乱整个数组public static int[] randomArr(int[] arr) {Random r new Random();for (int i 0; i arr.length; i) {//随机生成两个坐标int pos1 r.nextInt(arr.length);int pos2 r.nextInt(arr.length);int tmp arr[pos1];arr[pos1] arr[pos2];arr[pos2] tmp;}return arr;}//输出整个数组public static void printArr(int[] arr) {System.out.print([);for (int i 0; i arr.length; i) {if (i ! arr.length - 1) {System.out.print(arr[i] ,);} else {System.out.print(arr[i]);}}System.out.println(]);}
}程序运行结果如下 [2,7,4,3,9,6,8,5,1] [2,7,4,3,9,6,8,5,1] [2,7,4,3,9,6,8,5,1] [2,7,4,3,9,6,8,5,1] [2,7,4,3,9,6,8,5,1] 此时会发现二维数组的输出结果都是一样的通过打印地址会发现输出的地址都是一致的。
于是我们很快的可以想到方法传递数组是传递的地址值也就是最终我们在原数组中进行了修改然后返回他res数组里面的元素就都是arr所以最终的值是一样的那么我们又该如何解决呢
可以对返回的数组重新给他开辟一片空间即可。注意最后拷贝的时候不能是直接用数组名进行赋值用数组名最终是将新创建的数组指向传进来的那个数组我们需要的是拷贝每一个元素
import java.util.Random;public class Test {public static void main(String[] args) {int[] arr {1, 2, 3, 4, 5, 6, 7, 8, 9};int[][] res new int[5][arr.length];for (int i 0; i res.length; i) {//后面打乱依赖前面打乱生成的res[i] randomArr(arr);}//打印resfor (int i 0; i res.length; i) {printArr(res[i]);}}//打乱整个数组public static int[] randomArr(int[] arr) {Random r new Random();int []resnew int[arr.length];for (int i 0; i arr.length; i) {//随机生成两个坐标int pos1 r.nextInt(arr.length);int pos2 r.nextInt(arr.length);int tmp arr[pos1];arr[pos1] arr[pos2];arr[pos2] tmp;}//拷贝元素不能直接用resarr;for (int i 0; i arr.length; i) {res[i]arr[i];}return res;}//输出整个数组public static void printArr(int[] arr) {System.out.print([);for (int i 0; i arr.length; i) {if (i ! arr.length - 1) {System.out.print(arr[i] ,);} else {System.out.print(arr[i]);}}System.out.println(]);}
} 文章转载自: http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.bysey.com.gov.cn.bysey.com http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.hrpmt.cn.gov.cn.hrpmt.cn http://www.morning.kbgzj.cn.gov.cn.kbgzj.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.ldcrh.cn.gov.cn.ldcrh.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.pjfmq.cn.gov.cn.pjfmq.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.wmgjq.cn.gov.cn.wmgjq.cn http://www.morning.snygg.cn.gov.cn.snygg.cn http://www.morning.pctql.cn.gov.cn.pctql.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.kaweilu.com.gov.cn.kaweilu.com http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.ssqwr.cn.gov.cn.ssqwr.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.rydhq.cn.gov.cn.rydhq.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.ptxwg.cn.gov.cn.ptxwg.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.mrbzq.cn.gov.cn.mrbzq.cn http://www.morning.gynkr.cn.gov.cn.gynkr.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.kzqpn.cn.gov.cn.kzqpn.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.jpmcb.cn.gov.cn.jpmcb.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.zlhcw.cn.gov.cn.zlhcw.cn http://www.morning.xwgbr.cn.gov.cn.xwgbr.cn http://www.morning.webife.com.gov.cn.webife.com http://www.morning.ysgnb.cn.gov.cn.ysgnb.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.mmjyk.cn.gov.cn.mmjyk.cn http://www.morning.cnqdn.cn.gov.cn.cnqdn.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.rszyf.cn.gov.cn.rszyf.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.xmnlc.cn.gov.cn.xmnlc.cn http://www.morning.zgpgl.cn.gov.cn.zgpgl.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.nflpk.cn.gov.cn.nflpk.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.rjyd.cn.gov.cn.rjyd.cn