Results 1 to 7 of 7
  1. #1
    wynects is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default need help in ArrayList

    Example Question :

    • A constructor (with parameters) that assigns values to all data members.

    • Getters for all of the data members above.

    • A private static ArrayList named list that holds a list of Student objects.

    • A static getter named getList() that returns the ArrayList above.

    The things i have done :

    public class Student{
    String name;
    String gender;
    int year;
    String[] countries;

    public void setName(String name) {
    this.name = name;
    }
    public String getName() {
    return name;
    }




    I want to ask how to do the "ArrayList named list that holds a list of Student objects" and did i do any things wrong in declaring the variable for Student?:(:(

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    8

    Default

    Do you know how to declare a static variable? And do you know how to use ArrayList? Basically, what the part you've emphasized is asking is to declare a static variable named list, and that is an ArrayList type object meant to hold Student objects.

    Here's a couple links to help you:
    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects) (on static variables)
    Java: ArrayList (on ArrayList stuff--you can see him creating and modifying ArrayLists here)

  3. #3
    wynects is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    i still confusing with it :( can you make some simple example for me for the 4steps?(name+gender)

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    8

    Default

    If you clicked the links I provided, you'd find examples of everything you have to do. All that's left is for you to put 2 and 2 together. I can't sit behind you and help you during an exam--if you don't bother to learn HOW to do it, you're not going to have a whole lot of luck in the future, I'm afraid.

  5. #5
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    10

    Default

    You better make your class variables as private.It makes some sense in your class.
    Mak
    (Living @ Virtual World)

  6. #6
    Louis is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    • A private static ArrayList named list that holds a list of Student objects.

    import java.util.ArrayList;


    public class Student{
    private static ArrayList<Student> list = new ArrayList<Student>();
    }

    If I am wrong with this statement please help me do some correction.
    Thank you..^^

  7. #7
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    10

    Default

    Java Code:
    public class ListEx {
    	
    	
    	public static void main(String[] args) {	
    		
    		
    		new Student("mak","male");
    		
    		new Student("pandian","male");
    		
    		new Student("nithya","female");
    		
    		ArrayList list=Student.getList();
    		
    		for(Object o : list){
    			
    			Student s=(Student)o;
    			
    			System.out.println(s.getName());
    			
    			System.out.println(s.getGender());						
    			
    		}
    		
    	
    		
    		
    	}
    
    }
    
    
    class Student {
    	
    	
    private String name;
    	
    	private String gender;
    	
    	private static ArrayList<Student> list=new ArrayList<Student>();
    	
    	
    	public Student(String name,String gen) {		
    		this.gender=gen;
    		this.name=name;	
    		list.add(this);
    	}
    	
    
    	public String getName() {
    		return name;
    	}
    
    	
    
    	public String getGender() {
    		return gender;
    	}
    
    
    	public static ArrayList getList() {
    		return list;
    	}
    	
    	
    }
    Hope this helps..
    Mak
    (Living @ Virtual World)

Similar Threads

  1. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 04:34 PM
  2. Help with arraylist
    By alexisasoxfan in forum New To Java
    Replies: 12
    Last Post: 05-07-2010, 07:07 AM
  3. Arraylist
    By gnarly hogie in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 02:59 AM
  4. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  5. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 08:30 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
  •