0

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 + ">");
    }
}
3
  • Because set() method does not add a new element, it replaces the element at the specified position: docs.oracle.com/javase/1.5.0/docs/api/java/util/… Commented Feb 12, 2015 at 15:22
  • Your List seems to be empty, there is no .add() in your code so music.size() will be 0 and the code inside your for loop is never executed. Commented Feb 12, 2015 at 15:23
  • sorry I forgot to include the code where I added elements to my ArrayList. Commented Feb 12, 2015 at 15:35

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.