如何编写并行阵列 [英] how to write a parallel array
问题描述
大家好,我就怎么一会写一个code为一个数组,将为了匹配previous阵列的输入指标排序只是好奇。
hi everyone I'm just curious on how one would write a code for an array that will sort indexes in order to match a previous array's inputs.
我的意思例如:
如果阵列1曾在此顺序输入:
If array 1 had inputs in this order:
1,2,3,4
如何索引变量会看在阵列1
How the indexed variables would look in array 1
A[0] = 1
A[1] = 2
A[2] = 3
A[3] = 4
那么请问用户输入从previous阵列一批这样他们就可以添加信息。
Then i ask a user to input a number from the previous array so they can add information to.
user input = 2
现在我问他们想要加入到这一类什么样的信息?
Now i ask what information do they want to add to that category?
他们输入:苹果
我会希望发生的:
Array 1:
A[1] = 2
Array 2:
A[1] = apples
释编码:(请无视,如果我忘了的东西)
Explanation in coding: (please disregard if i forgot something)
import java.util.Arrays
import java.util.Scanner;
public static parallelArrays {
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
//arrays and declarations
int [] numbers = new int [4];
double [] money = new double [4];
system.out.println("Please enter 4 digits");
for (int i = 0 ; i < numbers.length; i++) {
numbers[i] = input.nextInt();
}
system.out.println("please choose the number you would want to add more information
to");
for (int i : numbers)
{
System.out.println(i + "; ");
}
//this is where I'm lost
//how should i write the code to get user input that will align with array "numbers"
// index?
}
}
我想创造的东西,发生在投入上双的形式,并能调整他们在阵数字已经收录了变量的顺序。这样我可以保持相互对应的数组索引。
i would like to create something that takes in inputs in double form and is able to align them in the order that array "numbers" has indexed its variables. so that i may keep the array indexes corresponding to each other.
将如何编码的面貌得到阵列2的指数,以自己在正确的顺序进行排序,以便将为了让他们在这样排序?匹配阵列1的指数
how would the coding look to get the index of array 2 to sort itself in the correct order so that it will match the index of array 1 in order to keep them sorted in this way?
很抱歉,如果这是一个简单的问题,我真的无法找到一个方法来做到这一点。
谢谢你。
Sorry if this is a trivial question, i really can't find a way to do so. thank you.
推荐答案
根据,如果我理解正确,你...
Depending on if I am understanding you correctly...
下面是我会推荐。让两个数组, A1 []
和 A2 []
。他们都将是相同的大小。
Here is what I would recommend. Make two arrays, A1[]
and A2[]
. They will both be the same size.
System.out.println("please choose the number you would want to add more information to");
int in = input.nextInt();
int index;
for (int i = 0; i < A1.length; i++){
if (A1[i] == in)
index = i;
}
System.out.println("Enter the new entry:");
A2[index] = input.next();
这将得到数组值,用户要在 A1
(由在存储
)和搜索的改变在值A1 []
。值的指数 A1 []
保存在首页
键,用于寻找<$ c中的相同位置$ C> A2 [] 。然后在 A2 []
新的输入要求。
This will get the array value that the user want the change in A1
(stored by in
) and search for the value in A1[]
. The index of the value in A1[]
is saved in index
and used to find the same position in A2[]
. the new input in A2[]
is then asked for.
希望这有助于。干杯。
这篇关于如何编写并行阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!