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 2 of 2

Thread: ArrayList.size()

  1. #1
    Junior Member
    Join Date
    Nov 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question ArrayList.size()

    I have a bit of problem...

    this case works:

    ArrayList<Bullet> bullets = new ArrayList<>();

    while(true){
    System.out.println(bullets.size());

    }
    it prints out number of elements progressively (other part of code adds objects into arraylist)
    but when I put bullets.size() in statement, it prints nothing even though size of arraylist increases

    ArrayList<Bullet> bullets = new ArrayList<>();

    while(true){

    if(bullets.size() > 0){
    System.out.println(bullets.size());
    }

    }

    like I dont understand what is the problem...it doesnt work in loops as well

    edit: I tried to create own method instead of size() but when I called it at the same place in statement it didnt work as well... what could possibly be th reason
    Last edited by NemTudom; November 5th, 2022 at 02:14 PM.

  2. #2
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: ArrayList.size()

    public class ArbitraryClass {
      private Integer someValue = 0;
     
      ArbitraryClass(Integer construction_initializer){
        someValue = construction_initializer;
      }
     
    } // ArbitraryClass
    import java.util.ArrayList;
     
    public class Scratchpad {
      public static void main(String[] args) {
     
        ArrayList<ArbitraryClass> myList = new ArrayList<>();
        myList.add(new ArbitraryClass(2));
        myList.add(new ArbitraryClass(4));
        myList.add(new ArbitraryClass(6));
     
        System.out.println(myList.size());
        System.out.println(myList.size()+10);
        System.out.println(
          String.format("Count of elements in myList: %s",
            myList.size()
          )
        );
     
      } // main
    } // Scratchpad

    Quote Originally Posted by output
    3
    13
    Count of elements in myList: 3
    Last edited by AngleWyrm; November 5th, 2022 at 08:35 PM.

Similar Threads

  1. Jpanel preffered size exceed the nactual screen size
    By manish.ctae@gmail.com in forum AWT / Java Swing
    Replies: 2
    Last Post: October 31st, 2012, 01:29 AM
  2. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM
  4. [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
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM