Page 1 of 2 12 LastLast
Results 1 to 20 of 32

Thread: array list help

  1. #1
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default array list help

    i have the following Name class

    Java Code:
    /**
     * A name has a first name and a family name.
     * There is an option to produce a full name by combining these.
     * 
     */
    public class Name {
    
    	//Fields
    	private String firstName;
    	private String familyName;
    
    	
    	//Constructors
    	public Name() {
    		firstName = "";
    		familyName = "";
    	}
    
    	
    	public Name(String firstName, String familyName) {
    		this.firstName = firstName;
    		this.familyName = familyName;
    	}
    
    	
    	//Methods
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    
    	public void setFamilyName(String familyName) {
    		this.familyName = familyName;
    	}
    
    	public String getFirstName() {
    		return firstName;
    	}
    
    	public String getFamilyName() {
    		return familyName;
    	}
    
    	public String getFullName() {
    		return firstName + " " + familyName;
    	}
    
    	public String toString() {
    		return "Name:[firstName=" + firstName + ", familyName=" + familyName + "]";
    	}
    }
    To test this class I want to create an array list and populate the first and family name using a scanner for 4 name, i then want to print out the array list i have tried:

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class RegisterTest {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    			
    			   int n = 0;
    		        int count;
    		        ArrayList<Name> register = new ArrayList<Name>(n);
    				Scanner in = new Scanner(System.in);
    
    		        System.out.print("How many names are you going to enter? ");
    		        n = in.nextInt();
    		        
    			for (int i = 0; i < n; i++)
    	        {   
    	            System.out.print("Enter the first name: ");
    	            String firstName = in.next();
    	           
    	            
    	            System.out.print("Enter the family name: ");
    	            String familyName = in.next();
    	            
    	        }
    			System.out.println(register);
    
    			
    	}
    	}
    but how would i add the firstname after the input i tried register.add(firstName);

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,322
    Rep Power
    5

    Default Re: array list help

    register is an ArrayList of type Name. So you need to create instances of Name with the first and last names. Then add that instance to the
    list. And you can't print out the entire register by just printing register. I suggest you iterate over the list using a for loop.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: array list help

    to print it out I was thinking about

    for (Name n1 : register) {
    System.out.println(n1.getFullName());
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: array list help

    Quote Originally Posted by jim829 View Post
    And you can't print out the entire register by just printing register. I suggest you iterate over the list using a for loop.
    Yes you can; ArrayLists are quite clever.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: array list help

    what do you mean exactly by create instances of name - as in new Name n1

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,322
    Rep Power
    5

    Default Re: array list help

    Sigh! I didn't realize that (or once knew it and forgot).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,322
    Rep Power
    5

    Default Re: array list help

    That's right of course!! I use it all the time via Arrays.asList() to print out an array. I was thinking the opposite so I got it bass-ackwards.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,322
    Rep Power
    5

    Default Re: array list help

    Name name = new Name(firstName, lastName). Forgive me for asking but you did write the code didn't you? Otherwise, why would you include
    a constructor you don't know how to use? Or is it the instance terminology with which you are unfamiliar?

    Regards,
    Jim
    Last edited by jim829; 11-08-2013 at 06:05 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: array list help

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class RegisterTest {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    			
    			   int n = 0;
    		        int count;
    		        ArrayList<Name> register = new ArrayList<Name>();
    				Scanner in = new Scanner(System.in);
    
    		        System.out.print("How many names are you going to enter? ");
    		        n = in.nextInt();
    		        
    		        Name n1 = new Name();
    		        
    		        
    			for (int i = 0; i < n; i++)
    	        {   
    	            System.out.print("Enter the first name: ");
    	            String firstName = in.next();
    	            register.add(n1);
    	            
    	            
    	            System.out.print("Enter the family name: ");
    	            String familyName = in.next();
    	            register.add(n1);
    	            
    	        }
    			for (Name n1  : register) { 
    				 System.out.println(n1.getFullName()); 
    				} 
    
    
    			
    	}
    	}
    i tried this but i get a duplicate variable on the for loop to print out the register - what is wrong? - if i assign a new name there is no return

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,322
    Rep Power
    5

    Default Re: array list help

    That works! But but as Jos stated, what you had will also work.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: array list help

    yes but instead of assigning a firstname and lastname i want to assign the values using the scanner class and input in the array list i am new to java jsut getting my head around classes

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,322
    Rep Power
    5

    Default Re: array list help

    Okay. That's fine! Just use the scanner as you had planned to input the first and last names. Then use those values to create an instance of Name like I explained. Then add that instance to the arraylist. Once you are done, you can then printout the list using whatever technique you like.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: array list help

    i tried to simplify with this

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class RegisterTest {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    			
    			 
    		        ArrayList<Name> register = new ArrayList<Name>();
    				Scanner in = new Scanner(System.in);
    		        
    		        for (int i=0; i < 4; i++ ) {
    					register.add(new Name());
    					System.out.print("Enter the first name: ");
    		            String firstName = in.next();
    		            System.out.print("Enter the family name: ");
    		            String familyName = in.next();
    				}
    			
    			for (Name n1  : register) { 
    				System.out.println("Hello"); 
    				 System.out.println(n1.getFullName()); 
    				 
    				} 
    	
    	}
    	}
    on the for loop to print the array list what should the variable name be? and is register.add(new Name()); ? is the code i have adding the first and family name to the array list?

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: array list help

    Your code doesn't make any sense: you create a Name object that doesn't have any names in it (see your own no-args constructor) and you add it over and over again to that List while effectively ignoring the fist name and last name just read. Are you sure you wrote the code for the Name class yourself? b.t.w. simplification (as in optimization) is futile as long as your code doesn't work.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    54
    Rep Power
    0

    Default Re: array list help

    You have created an ArrayList register.
    You have created a class Name to hold the names
    You have created a scanner to read the names from the concole int o temporary variables firstName and lastName
    You have skipped a step

  16. #16
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: array list help

    Quote Originally Posted by ras_oscar View Post
    You have created an ArrayList register.
    You have created a class Name to hold the names
    You have created a scanner to read the names from the concole int o temporary variables firstName and lastName
    You have skipped a step
    thank you for a sensible reply instead of i dont think you coded the clas, I just started learning java give me a break if I dont know where and when to declare things.

    Anyway is the skipped step transfer from variables to be stored in the list e.g. from firstName to ArrayList register?

    thanks

  17. #17
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,322
    Rep Power
    5

    Default Re: array list help

    Quote Originally Posted by ahanslot View Post
    thank you for a sensible reply instead of i dont think you coded the clas
    Ahem, all of our replies are sensible. However, a constructor and its use are basic to declaring and using a class. So it is difficult for us
    to imagine being able to write a class with multiple constructors and not know how to use it. In any event, we take you at your word that
    you wrote it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    54
    Rep Power
    0

    Default Re: array list help

    You need to create an instance of your Name class and put the data into it, then add the instance to ArrayList register.Something like this:

    Name T = new Name();
    T.familyName=familyName;
    T.firstName=firstName;
    register.add(T);

    If I were coding it, I'd using other local variable names to avoid confusion, but that's up to you.

    Please understand, many new members come here and dump their homework, expecting to have someone do it for them. The tendency of members is to not post code, but to discuss concepts. I have just broken that unwritten rule and expect to be punished severely, but i will survive.

    It took me a while to get used to this, but in the long run It has helped me understand a great deal more than getting code handed to me.

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,322
    Rep Power
    5

    Default Re: array list help

    Well, that wouldn't work in this case because the fields are private. But the OP already has the perfect constructor.

    Name name = new Name(firstName, familyName);

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: array list help

    Quote Originally Posted by jim829 View Post
    Well, that wouldn't work in this case because the fields are private. But the OP already has the perfect constructor.

    Name name = new Name(firstName, familyName);

    Regards,
    Jim
    Thanks for the help guys this is the code i used, instead of using the set methods that I created in the class,
    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class RegisterTest {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    			
    			 
    		        ArrayList<Name> register = new ArrayList<Name>();
    				Scanner in = new Scanner(System.in);
    		        
    		        for (int i=0; i < 2; i++ ) {
    					System.out.print("Enter the first name: ");
    		            String firstName = in.next();
    		            System.out.print("Enter the family name: ");
    		            String familyName = in.next();
    		            Name T = new Name();
    		            T.setFamilyName(familyName);
    		            T.setFirstName(firstName);
    		            register.add(T);
    				}
    			
    		        //return array list
    			for (Name n1  : register) { 
    				System.out.println("Hello"); 
    				 System.out.println(n1.getFullName()); 
    				 
    				} 
    	
    	}
    	}
    I also tried using the custom constructor I created to simplify the code:

    Java Code:
     Name T = new Name(firstName, familyName);
    		            register.add(T);
    Both work perfect :)

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with Array List
    By UnTil_Fate in forum New To Java
    Replies: 4
    Last Post: 01-26-2013, 03:30 PM
  2. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Array List
    By mashyum in forum Advanced Java
    Replies: 2
    Last Post: 07-28-2009, 06:47 AM
  5. Array List
    By mprentice84 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 02:12 AM

Posting Permissions

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