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。