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)。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP