Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Question about ArrayList

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about ArrayList

    I'm trying to make a sort of theoretical MP3 player for an assignment, using the ArrayList class

    Basically, I have a "Song" class that defines attributes of songs, including Title, Artist, Duration, and most importantly, Filesize. I have to use an ArrayList to store the songs, but this music player has a size limit

    My question is, how do I get the ArrayList to specify a capacity limit, and then check the 'filesize' variable when determining how full it is? I'm really only familiar with the trimToSize method, which isn't what I need. If the limit is 15, I don't want 15 song objects, I want it to fill up based on the object's 'filesize' attribute

    I hope that made any sort of sense


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Question about ArrayList

    How does a file's size relate to the capacity of an ArrayList? An ArrayList holds references to objects, not the full contents of an object.

    Are you asking how to limit the number of objects that can be placed in an ArrayList based on the size of the objects?


    What are you trying to do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about ArrayList

    I have to create an ArrayList that can hold Song objects. Each Song object has an attribute, fileSize, that says, well, how big the file is. This ArrayList can hold any number of songs, but only as long as the total fileSize of those songs doesn't exceed a certain amount (To be set by me, say 15)

    I'm not sure how to get that to work

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Question about ArrayList

    One way would be to wrap the ArrayList in a class and have an add() method that would look at the filesize of items being added and control the number allowed based on total filesize so far.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Arraylist input help, quick question.
    By Sarmen in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 11th, 2012, 01:37 PM
  2. ArrayList question
    By steel55677 in forum Collections and Generics
    Replies: 8
    Last Post: February 25th, 2012, 10:42 AM
  3. Please help ArrayList Question
    By SandeeBee in forum Collections and Generics
    Replies: 14
    Last Post: November 14th, 2011, 11:01 PM
  4. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM