Java 用其对应的等级替换数组中的元素
在Java中,数组是一个对象。它是一种非原始数据类型,可以存储类似数据类型的值。
根据问题陈述,我们给出了一个带有一些随机整数值的数组,我们必须用它们相应的等级来替换这些元素。因此,我们首先要对给定的数组进行升序排序以找到等级。在找到等级后,我们只需将这些等级值替换成各自数组的元素。
让我们来探讨一下这篇文章,看看如何通过使用Java编程语言来实现。
向你展示一些实例
实例1
给定数组= [12,23,34,45,15]。
通过对给定数组进行升序排序= [12,15,23,34,45] 。
所以,如果我们用相应的数组元素替换这些元素,我们得到=[1,3,4,5,2]
实例-2
给定数组= [38,94,86,63,36]。
通过对给定数组进行升序排序= [36,38,63,86,94]
所以,如果我们用相应的数组元素来替换这些元素,我们得到= [2, 5, 4, 3, 1]
实例-3
给定数组= [54,67,23,95,24]。
通过对给定数组进行升序排序= [23,24,54,67,95] 。
所以,如果我们用相应的数组元素来替换这些元素,我们得到= [3, 4, 1, 5, 2]
算法
- 第1步 – 声明一个数组,并用一些随机的整数值来初始化它。
-
第2步 – 声明另一个数组,这是一个临时数组,存储输入数组的复制元素。
-
第3步– 将这个临时数组按升序排序,这样我们就可以得到等级。
-
第4步 – 采取一个嵌套的for循环,将等级与给定数组中的相应数组元素进行比较,并将等级存储在它们之前的值中。
-
第5步 – 在得到整个数组的替换等级后,打印该数组作为输出。
语法
要获得一个数组的长度(该数组中的元素数),有一个数组的内置属性,即 长度。
下面是它的语法
array. length
其中,’array’指的是数组引用。
你可以使用Arrays.sort()方法将数组按升序排序。
Arrays.sort(array_name);
你可以使用Arrays.copyOfRange()方法来复制一个数组的元素。
Arrays.copyOfRange(array_name, 0, array_name.length);
多种方法
我们提供了不同方法的解决方案。
- 通过使用嵌套for循环
-
通过用户哈希图
让我们逐一看看这个程序及其输出。
方法1:通过使用嵌套的循环方法
在这种方法中,我们用一些随机的整数值声明一个数组,通过使用嵌套的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替换数组元素。