亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對隨機生成的數字的向量進行排序

對隨機生成的數字的向量進行排序

泛舟湖上清波郎朗 2023-02-23 17:58:17
我試圖解決一個問題,但我找不到為什么我的代碼不能解決這個問題。我生成了一個包含 100 個元素的隨機向量,我試圖將它們排列成另一個。不知何故,我新生成的向量充滿了隨機向量的最后一個索引值。int[] vetorAleatory = new int[100];for (int i = 0; i < vetorAleatory.length; i++) {    vetorAleatory[i] = new Random().nextInt(1000);}int[] vetorByOrder = new int[100];int newVetorPosition = 0;for (int i = 0; i < 100; i++) {    for (int x = 0; x < 100; x++) {        vetorByOrder[newVetorPosition] = 2000;        if (vetorAleatory[i] < vetorByOrder[newVetorPosition]) {            boolean newEntry = true;            for (int y = 0; y < newVetorPosition; y++) {                if (vetorByOrder[y] == vetorByOrder[newVetorPosition]) {                    newEntry = false;                    break;                }            }            if (newEntry == true) {                vetorByOrder[newVetorPosition] = vetorAleatory[x];            }        }        if (x == 99) {            newVetorPosition++;        }    }}for (int i = 0;i<100;i++) {    System.out.print(vetorAleatory[i] + ", " + vetorByOrder[i] + System.lineSeparator());}
查看完整描述

1 回答

?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

首先,您不需要 3 個循環來對數組進行排序。您只需要 2 個,在快速搜索的情況下,它甚至更少。


查看完整回答
反對 回復 2023-02-23
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號