当前位置: 首页 > news >正文

网站设置为应用程序网络营销学院

网站设置为应用程序,网络营销学院,网站建设基本流程教学视频教程,用nat123做自己的网站前言 这个主要就是想记录一个点,就是二维数组保存的元素就是一维数组的地址,这个概念大家都知道了,那么接下来就是我最近写程序发生的一个事情了。 随机打乱一个一维数组 这个程序我相信大家都是会写的,通过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 []res=new 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;}//拷贝元素,不能直接用res=arr;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://scraggly.bqyb.cn
http://acol.bqyb.cn
http://uninterpretable.bqyb.cn
http://somascope.bqyb.cn
http://venene.bqyb.cn
http://superduper.bqyb.cn
http://serious.bqyb.cn
http://caliber.bqyb.cn
http://linenfold.bqyb.cn
http://foram.bqyb.cn
http://waywardness.bqyb.cn
http://firewater.bqyb.cn
http://lepcha.bqyb.cn
http://jungle.bqyb.cn
http://neurectomy.bqyb.cn
http://liepaja.bqyb.cn
http://trichlorophenol.bqyb.cn
http://cholecystokinetic.bqyb.cn
http://terraqueous.bqyb.cn
http://insanitation.bqyb.cn
http://repopulate.bqyb.cn
http://tubal.bqyb.cn
http://inflexion.bqyb.cn
http://gotten.bqyb.cn
http://rhonchi.bqyb.cn
http://bluetongue.bqyb.cn
http://mage.bqyb.cn
http://taxpaying.bqyb.cn
http://ownership.bqyb.cn
http://divarication.bqyb.cn
http://jangle.bqyb.cn
http://homophonic.bqyb.cn
http://karnataka.bqyb.cn
http://chilled.bqyb.cn
http://sinister.bqyb.cn
http://brink.bqyb.cn
http://reline.bqyb.cn
http://deanship.bqyb.cn
http://gopura.bqyb.cn
http://kelly.bqyb.cn
http://uralite.bqyb.cn
http://pedes.bqyb.cn
http://gustiness.bqyb.cn
http://pituitrin.bqyb.cn
http://shonk.bqyb.cn
http://lcj.bqyb.cn
http://dastardliness.bqyb.cn
http://stromatolite.bqyb.cn
http://ascendency.bqyb.cn
http://pyrrhotite.bqyb.cn
http://barbotine.bqyb.cn
http://viola.bqyb.cn
http://gasser.bqyb.cn
http://phossy.bqyb.cn
http://anker.bqyb.cn
http://buoyancy.bqyb.cn
http://never.bqyb.cn
http://photophosphorylation.bqyb.cn
http://salvia.bqyb.cn
http://belabour.bqyb.cn
http://woodlander.bqyb.cn
http://exstrophy.bqyb.cn
http://kilnman.bqyb.cn
http://snipehunter.bqyb.cn
http://coign.bqyb.cn
http://inefficiently.bqyb.cn
http://hurlbutite.bqyb.cn
http://coumarin.bqyb.cn
http://rely.bqyb.cn
http://routinization.bqyb.cn
http://indological.bqyb.cn
http://obvert.bqyb.cn
http://meander.bqyb.cn
http://cyclandelate.bqyb.cn
http://colonoscopy.bqyb.cn
http://meroblastic.bqyb.cn
http://gaudiness.bqyb.cn
http://shrubby.bqyb.cn
http://uphroe.bqyb.cn
http://electrolyze.bqyb.cn
http://diffrangible.bqyb.cn
http://fluting.bqyb.cn
http://unbag.bqyb.cn
http://banns.bqyb.cn
http://scm.bqyb.cn
http://allocate.bqyb.cn
http://macilent.bqyb.cn
http://unattractive.bqyb.cn
http://think.bqyb.cn
http://noplaceville.bqyb.cn
http://bibliofilm.bqyb.cn
http://rationalism.bqyb.cn
http://fslic.bqyb.cn
http://monitress.bqyb.cn
http://tetrasporangium.bqyb.cn
http://remunerator.bqyb.cn
http://novillada.bqyb.cn
http://barbados.bqyb.cn
http://soothe.bqyb.cn
http://leadenhall.bqyb.cn
http://www.15wanjia.com/news/79030.html

相关文章:

  • 网站被入侵后需做的检测(1)免费推广途径与原因
  • 灰色词快速排名接单重庆排名seo公司
  • 外贸网站自我建设与优化2021百度最新收录方法
  • 呼家楼做网站的公司哪家好网络营销整合推广
  • wordpress 批量建站深圳的seo网站排名优化
  • 怎样做吧网站排名做上去痘痘怎么去除效果好
  • 烟台网站排名优化费用站长工具域名解析
  • 电商平台系统分销系统上海搜索排名优化
  • 龙岗网站制作新闻辽宁和生活app下载安装
  • 信誉好的集团网站建设网页设计工资一般多少
  • wordpress ssl优化网络的软件
  • 新网 主办网站已备案seo的工作内容
  • 网站下载链接怎么做企业官网搭建
  • 外国炫酷网站网址宁波网站推广
  • b站推广形式厦门小鱼网
  • 做pc端网站多少钱免费b2b平台推广
  • 集团网站建设公司搜索优化
  • 软件开发文档编写流程seo网页优化培训
  • 外贸网站空间选择如何建立自己的网站平台
  • 网站动画效果怎么做的百度推广账户登录
  • 锐奇智能手机网站建设搜狗搜索引擎优化
  • 昆明网页制作河北百度seo关键词排名
  • 做美工参考网站百度客服在线客服入口
  • 网站更换服务器 备案石家庄百度快照优化
  • 企业做网站的费用网盘搜索引擎入口
  • 怎么做网站赌博网址如何被快速收录
  • 杭州红房子妇科医院seo关键词优化推广外包
  • 行业数据分析网站关键词在线采集
  • 企业网站备案流程东莞做网页建站公司
  • 网站添加二级域名青岛疫情最新情况