简体   繁体   English

通过随机数递增ArrayList元素

[英]Incrementing ArrayList elements by Random Numbers

So I'm working on an assignment for my AP Computer Science Class dealing with ArrayLists. 所以我正在为我的AP计算机科学课分配一个处理ArrayLists的作业。 For #13 and 14 I am supposed to increment each ArrayList element by a random number between 0 and 20 (inclusive). 对于#13和14,我应该将每个ArrayList元素增加0到20(含)之间的随机数。 I don't understand why I cannot use the (ArrayList.set()) method to increment my ArrayList elements. 我不明白为什么我不能使用(ArrayList.set())方法来增加ArrayList元素。 Thank you in advance! 先感谢您!

import java.util.Collection; //Driver Class
import java.util.Random;
import java.util.ArrayList;
import java.util.Random;
public class MusicDownloads {
    public static void main(String[] args) {
        ArrayList < DownloadInfo > music = new ArrayList < DownloadInfo > ();
    music.add(new DownloadInfo("So What"));                         //2
    music.add(new DownloadInfo("Blue in Green"));
    music.add(new DownloadInfo("Night in Tunisia"));
    music.add(new DownloadInfo("Fly Me to the Moon"));
    music.add(new DownloadInfo("Come Fly With Me"));
    music.add(new DownloadInfo("This Town"));
    music.add(new DownloadInfo("Flamenco Sketches"));

        int addNum = 0;
        Random rand = new Random();
        for (int i = 0; i < music.size(); i++) {
            addNum = 0;
            addNum = (int) Math.random() * 20; //13
            music.set(i, music.get(i).addNumDL(addNum));
        }


        System.out.println("Num 13/14");
        for (int i = 0; i < music.size(); i++) {
            System.out.println(music.get(i)); //14
        }
    }
}




public class DownloadInfo //Object Class
{
    private String title = "";
    private int numDownloads = 0;

    public DownloadInfo(String t) {
        title = t;
    }

    public String getSongTitle() {
        return title;
    }

    public void addNumDL(int addNum) {
        numDownloads = addNum + numDownloads;
    }

    public int getnumDownloads() {
        return numDownloads;
    }

    public String toString() {
        return ("Song title: <" + title + "> " + " <Number of times downloaded: " + numDownloads + ">");
    }
}

You should add(…) the elements rather than using set(i, …) . 您应该add(…)元素,而不要使用set(i, …)

Apart from that, a few style nitpicks: 除此之外,还有一些风格的nitpicks:

  • Don't use Math.random() if you want an integer. 如果需要整数,请不要使用Math.random()。 Rather create a Random (or use ThreadLocalRandom, if you're on 1.7 or later) and use its nextInt(int) method. 而是创建一个Random(或者,如果使用1.7或更高版本,则使用ThreadLocalRandom)并使用其nextInt(int)方法。 You even instantiate a Random above. 您甚至可以实例化上面的Random。
  • Java has a += operator which updates the value in place. Java有一个+ =运算符,可以在适当的位置更新值。

If you are simply trying to increment the counter within the DownloadInfo object as it appears in the code snippet, then you don't need to replace the element in the array. 如果您只是想在代码片段中显示的DownloadInfo对象中增加计数器,则无需替换数组中的元素。 You could simply use: 您可以简单地使用:

public static void main(String[] args)
{
   ArrayList<DownloadInfo> music = new ArrayList<DownloadInfo>();
   music.add(new DownloadInfo("Wasted Years"));
   music.add(new DownloadInfo("The Trooper"));
   music.add(new DownloadInfo("Can I Play with Madness"));
   music.add(new DownloadInfo("22 Acacia Avenue"));
   music.add(new DownloadInfo("Rime of the Ancient Mariner"));

   for (int i=0; i<music.size(); i++)
   {           
       int addNum = (int) (Math.random() * 20);
       music.get(i).addNumDL(addNum);
   }


    System.out.println("Num 13/14");
    for (int i=0; i<music.size(); i++)
    {
        System.out.println(music.get(i));       //14
    }
}   

Note the parenthesis around the (Math.random() * 20). 请注意(Math.random()* 20)周围的括号。 If you don't use these then the int cast is applied to the result of Math.random() and you end up with a lot of 0 results. 如果不使用它们,则将int强制转换应用于Math.random()的结果,最终会得到很多0结果。

In your code above when you are using set, the result of addNumDL is not a DownloadInfo object so I am assuming you are getting a compile issue there as you are trying to set a void into the array. 在上面的代码中,当您使用set时,addNumDL的结果不是一个DownloadInfo对象,因此我假设您在尝试向数组中设置void时遇到了编译问题。 At least that is what I got when I built your class locally. 至少那是我在本地建立您的课程时得到的。 If I am misunderstanding you and you are trying to move the objects around within the ArrayList as well we can go from there. 如果我对您有误解,并且您也尝试在ArrayList中移动对象,那么我们也可以从那里开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM