Ati Ok
Ati Ok

Reputation: 1

Incrementing ArrayList elements by Random Numbers

So I'm working on an assignment for my AP Computer Science Class dealing with ArrayLists. For #13 and 14 I am supposed to increment each ArrayList element by a random number between 0 and 20 (inclusive). I don't understand why I cannot use the (ArrayList.set()) method to increment my ArrayList elements. 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 + ">");
    }
}

Upvotes: 0

Views: 217

Answers (2)

Michael
Michael

Reputation: 539

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. 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). 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.

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. 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.

Upvotes: 1

llogiq
llogiq

Reputation: 14521

You should add(…) the elements rather than using set(i, …).

Apart from that, a few style nitpicks:

  • Don't use Math.random() if you want an integer. Rather create a Random (or use ThreadLocalRandom, if you're on 1.7 or later) and use its nextInt(int) method. You even instantiate a Random above.
  • Java has a += operator which updates the value in place.

Upvotes: 1

Related Questions