Results 1 to 8 of 8
  1. #1
    dazz is offline Member
    Join Date
    Apr 2017
    Posts
    27
    Rep Power
    0

    Default Adding a named custom object to an arraylist????

    Hi
    According to this example which includes the following code:
    Java Code:
    import java.util.*;  
    public class TestCollection3{  
     public static void main(String args[]){  
      //Creating user-defined class objects  
      Student s1=new Student(101,"Sonoo",23);  
      Student s2=new Student(102,"Ravi",21);  
      Student s2=new Student(103,"Hanumat",25);  
      //creating arraylist  
      ArrayList<Student> al=new ArrayList<Student>();  
      al.add(s1);//adding Student class object  
      al.add(s2);  
      al.add(s3);  
      //Getting Iterator  
      Iterator itr=al.iterator();  
      //traversing elements of ArrayList object  
      while(itr.hasNext()){  
        Student st=(Student)itr.next();  
        System.out.println(st.rollno+" "+st.name+" "+st.age);  
      }  
     }  
    }
    I should be able to store named custom objects in an arraylist.

    Java Code:
    public class  LoadFlags {
        private int i =0;  // counter
        private final int iMax = 30;// max number in the array
    
        private final static Logger log = LogManager.getLogger(LoadFlags.class);
        
        //the array  list 
        public ArrayList<FlagDescriptor> flagList = new ArrayList<FlagDescriptor>(iMax); //initial capacity of 30 objects
    
        FlagDescriptor blankFlag = new FlagDescriptor.FlagDescriptorBuilder().build();
    
     *   flagList.add(blankFlag);
    Netbeans gives error messages for the * line that indicate it can't find "flagList". It suggests creating or importing the class "flagList".
    The error message says:
    Java Code:
    <identifier> expected.
    Cannot find symbol class blankFlag
    Location class  LoadFlags
    Package flagLists does not exist
    I can't see what I am doing wrong. Any suggestions??

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Adding a named custom object to an arraylist????

    That line does not seem to be in a code block.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    dazz is offline Member
    Join Date
    Apr 2017
    Posts
    27
    Rep Power
    0

    Default Re: Adding a named custom object to an arraylist????

    Hi
    The class is about 250 lines long so I pruned it severely for posting.
    I have wrapped the buggy code with
    <code>
    public static void main(String args[]){
    ...
    }
    </code>
    that worked.
    thanks

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Adding a named custom object to an arraylist????

    flagsList is outside of a method, while it should be inside one (or a constructor).

    Then again, if you strip out too much code, we can't be sure...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Adding a named custom object to an arraylist????

    A couple observations:

    Line 14. Use
    Java Code:
    Iterator<Student> itr = al.iterator();
    Then you won't have to cast the return from itr.next().

    When using Interface implementations (e.g. ArrayList for List) it is considered best practice to assign to
    the interface type rather than the implementation type.

    e.g
    Java Code:
    List<Student> list = new ArrayList<>();
    Exceptions to the practice are when the implementation provides additional functionality that the interface
    doesn't know about.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Adding a named custom object to an arraylist????

    Quote Originally Posted by jim829 View Post
    When using Interface implementations (e.g. ArrayList for List) it is considered best practice to assign to
    the interface type rather than the implementation type.
    That's the fault of the tutorial. "Contact us" shows a number that starts with +91. That is India...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Adding a named custom object to an arraylist????

    Yeah. And I looked at the second example and they assigned the ArrayList to a List type.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    dazz is offline Member
    Join Date
    Apr 2017
    Posts
    27
    Rep Power
    0

    Default Re: Adding a named custom object to an arraylist????

    Quote Originally Posted by SurfMan View Post
    flagsList is outside of a method, while it should be inside one (or a constructor).

    Then again, if you strip out too much code, we can't be sure...
    I made a basic mistake. No constructor or methods defined.

Similar Threads

  1. NetBeans 7.2 RC1 adding a custom hint
    By DarVar in forum NetBeans
    Replies: 1
    Last Post: 07-05-2012, 04:20 PM
  2. Adding custom controls in a C/C++ DLL
    By Settler in forum AWT / Swing
    Replies: 2
    Last Post: 08-26-2011, 04:19 PM
  3. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  4. Adding custom highlight to JEditorPane
    By andrewb in forum AWT / Swing
    Replies: 0
    Last Post: 06-22-2007, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •