[英]Using int[] arrays to manipulate each other through nested loops
What I am trying to do is use one method to generate 30 random numbers from 0-15 and then another method to count how many times each number prints and put it in a second array. 我想做的是使用一种方法从0-15生成30个随机数,然后使用另一种方法计算每个数字打印多少次并将其放入第二个数组中。 Each position in the second array corresponds to the number in the array it is.
第二个数组中的每个位置都对应于它所在的数组中的数字。 if i = [0] and 0 shows up 3 times, it should be 3 and so on.
如果i = [0]并且0出现3次,则应该为3,依此类推。
So far, I have gotten this. 到目前为止,我已经明白了。 What seems to be happening is that it counts the number of times 15 shows up (The last number of the array).
似乎正在发生的事情是,它计算15出现的次数(数组的最后一个数字)。 Though I could be wrong.
虽然我可能是错的。 I don't see what I'm doing wrong.
我看不到我在做什么错。 There must be something wrong with my logic.
我的逻辑肯定有问题。
import java.util.Arrays;
public class FrequencyOfNumbers {
public static void main(String[]args){
System.out.println(Arrays.toString(randomNums()));
System.out.println(Arrays.toString(sortedNums(randomNums())));
}
public static int[] randomNums (){
int[] random = new int[30];
for(int i=0;i<random.length;i++){
double randNum = Math.random() * 16;
random[i] = (int)randNum;
}
return random;
}
public static int[] sortedNums(int[] sort){
int[] numVals = new int[15];
for(int i=0;i<numVals.length;i++)
{
for(int j=0;j<sort.length;j++)
{
if(sort[j] == numVals[i])
{
numVals[i]++;
}
}
}
return numVals;
}
}
Example output I'm receiving: 我收到的示例输出:
[5, 15, 0, 5, 4, 10, 4, 11, 5, 13, 13, 8, 9, 9, 10, 6, 0, 9, 10, 12, 3, 7, 4, 9, 4, 11, 9, 15, 10, 7]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
To count the number of instances of an object in a collection you can use Collections.frequency()
: 要计算集合中对象的实例数,可以使用
Collections.frequency()
:
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < sort.length; i++) {
list.add(sort[i]);
}
for(int i = 0; i< numVals.length;i++){
numVals[i] = Collections.frequency(list, i);
}
return numVals;
You have some problems in the code: 您在代码中有一些问题:
Don't call randomNums()
twice. 不要两次调用
randomNums()
。 Otherwise you don't count the right frequency. 否则,您将无法计算正确的频率。 It will generate different arrays.
它将生成不同的数组。
If you want to keep numbers from 0 to 15, you should allocate 16 elements for numVals
. 如果要将数字保持在0到15之间,则应为
numVals
分配16个元素。
You don't need an inner for
loop when you count the appearances. 计算外观时,不需要内部
for
循环。 Just consider all the numbers, take their values, and increment the number of appearances. 只要考虑所有数字,取它们的值,然后增加出现次数。
Try this: 尝试这个:
public static void main(String[] args) {
int[] randomNumbers = randomNums();
System.out.println(Arrays.toString(randomNumbers));
System.out.println(Arrays.toString(sortedNums(randomNumbers)));
}
public static int[] randomNums() {
int[] random = new int[30];
for (int i = 0; i < random.length; i++) {
random[i] = (int) (Math.random() * 16);
}
return random;
}
public static int[] sortedNums(int[] sort) {
int[] numVals = new int[16];
for (int j = 0; j < sort.length; j++) {
numVals[sort[j]]++;
}
return numVals;
}
A possible output: 可能的输出:
[8, 6, 5, 12, 12, 9, 15, 6, 7, 9, 15, 3, 6, 7, 3, 8, 6, 3, 15, 8, 12, 4, 7, 12, 2, 15, 6, 5, 4, 5]
[0, 0, 1, 3, 2, 3, 5, 3, 3, 2, 0, 0, 4, 0, 0, 4]
Performance 性能
If you want to count the number of comparisons (practically) you can: 如果要(实际)计算比较次数,可以:
Create a new member in your class: 在您的班级中创建一个新成员:
static int steps = 0;
Define a new method that increments the counter: 定义一个增加计数器的新方法:
private static boolean f() { steps++; return true; }
Add f() &&
before the current condition in if
. 在
if
,在当前条件之前添加f() &&
。
Add the following last line in the main
method: 在
main
方法中添加以下最后一行:
System.out.println(steps);
Using only one for
you will generate 30
steps. 仅使用一个
for
你会产生30
步。
Using 2 for
loops (as you wrote in your comment) will generate 480
steps ( 480 = 30 * 16
). 使用2
for
循环(如您在注释中所写)将生成480
步( 480 = 30 * 16
)。
In this case is irrelevant because both operations are very fast. 在这种情况下无关紧要,因为两个操作都非常快。 But for a larger input if the first approach takes 1 second, you should care if the second one takes more than 10 seconds.
但是对于较大的输入,如果第一种方法花费1秒,则应注意第二种方法是否花费10秒以上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.