Java中用對應的排名替換陣列元素


在Java中,陣列是一個物件。它是一種非基本資料型別,用於儲存相同資料型別的多個值。

根據題目要求,我們得到一個包含一些隨機整數值的陣列,我們需要用其對應的排名替換這些元素。因此,我們首先需要將給定陣列按升序排序以找到排名。找到排名後,我們只需將這些排名值替換為它們各自陣列元素的值。

讓我們一起探索這篇文章,看看如何使用Java程式語言來實現。

展示一些示例

示例1

Given Array= [12,23,34,45,15].
By sorting the given array in ascending order= [12,15,23,34,45]
So, if we replace the elements with their corresponding array’s elements, we get = [1, 3, 4, 5, 2]

示例2

Given Array= [38,94,86,63,36].
By sorting the given array in ascending order= [36,38,63,86,94]
So, if we replace the elements with their corresponding array’s elements, we get = [2, 5, 4, 3, 1]

示例3

Given Array= [54,67,23,95,24].
By sorting the given array in ascending order= [23,24,54,67,95]
So, if we replace the elements with their corresponding array’s elements, we get = [3, 4, 1, 5, 2]

演算法

  • 步驟1 - 宣告一個數組並用一些隨機整數值初始化它。

  • 步驟2 - 宣告另一個作為臨時陣列的陣列,並存儲輸入陣列的複製元素。

  • 步驟3 - 將臨時陣列按升序排序,以便我們獲得排名。

  • 步驟4 - 使用巢狀for迴圈比較排名與其在給定陣列中對應的陣列元素,並將排名儲存在其先前值的位置。

  • 步驟5 - 獲取整個替換了排名的陣列後,將該陣列作為輸出列印。

語法

要獲取陣列的長度(陣列中元素的數量),陣列有一個內建屬性,即length

以下是它的語法:

array. length

其中,“array”指的是陣列引用。

您可以使用Arrays.sort()方法按升序排序陣列。

Arrays.sort(array_name);

您可以使用Arrays.copyOfRange()方法複製陣列的元素。

Arrays.copyOfRange(array_name, 0, array_name.length);

多種方法

我們提供了多種解決方案。

  • 使用巢狀for迴圈

  • 使用雜湊表

讓我們逐一檢視程式及其輸出。

方法1:使用巢狀for迴圈方法

在這種方法中,我們宣告一個包含一些隨機整數值的陣列,並使用巢狀for迴圈方法將陣列元素替換為其對應的排名,然後將該陣列作為輸出列印。

示例

import java.util.*;
public class Main {
   public static void main(String[] args) {
      
      // declare an integer type of array and store some random value to it by static input method
      int[] inputArray = { 122, 34, 25, 67 , 87};
      
      // call the user-defined method and pass the inputArray[]
      rankedArray(inputArray);
      
      // Print the array as output
      System.out.println("Array with replaced ranks = " + Arrays.toString(inputArray));
   }
   //user-defined method to replace the elements of given array by their ranks
   static void rankedArray(int[] inpArr) {
      // declare a temporary array and store the copied array of input array
      int temp[] = Arrays.copyOfRange(inpArr, 0, inpArr.length);
      // Sort the values of temp[] array in ascending order Arrays.sort(temp);
      //initiate the nested-loop to find the corresponding position of given array
      for(int i=0; i< inpArr.length; i++){
         for(int j=0; j< inpArr.length; j++){
            if(temp[j]==inpArr[i]){
               inpArr[i] = j+1;
               break;
            }
         }
      }
   }
}

輸出

Array with replaced ranks = [5, 2, 1, 3, 4]

方法2:使用雜湊表

在這種方法中,我們宣告一個包含一些隨機整數值的陣列,並使用雜湊表方法將陣列元素替換為其對應的排名,然後將該陣列作為輸出列印。

示例

import java.util.*;
public class Main {
   public static void main(String[] args) {
      
      // declare and initialize integer type array
      int[] inputArray = { 34,53,12,64,76};
      
      // call the user-defined method
      rankedArray(inputArray);
      
      // Print the array as output
      System.out.println("Array with replaced ranks = " +
      Arrays.toString(inputArray));
   }
   
   //user-defined method to replace the elements of given array by their ranks
   static void rankedArray(int[] inpArr) {
   
      // declare a temporary array and store the copied inputed array
      int temp[] = Arrays.copyOfRange(inpArr, 0, inpArr.length);
      
      // Sort the temp[] array in ascending order
      Arrays.sort(temp);
      
      // create object of HashMap class to store the maped rank of array's elements
      Map hashRanks = new HashMap<>();
      
      //declare an integer to store the value of corresponding Ranks of array's elements
      int correspondingRank = 1;
      
      //intiate the loop
      for (int i = 0; i < temp.length; i++) {
         
         //declare an integer to store the elements of array
         int num = temp[i];
         
         // Update the corresponding Ranks of array's elements
         if (hashRanks.get(num) == null) {
            hashRanks.put(num, correspondingRank);
            correspondingRank++;
         }
      }
      
      // Assign the corresponding Ranks to the array's elements
      for (int i = 0; i < inpArr.length; i++) {
         int num = inpArr[i];
         inpArr[i] = (int)hashRanks.get(inpArr[i]);
      }
   }
}

輸出

Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Array with replaced ranks = [2, 3, 1, 4, 5]

在這篇文章中,我們探討了如何使用Java程式語言將陣列元素替換為-6(如果任何元素的最後一位數字是6)。

更新於:2023年2月2日

1K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.