CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2016
    Posts
    1

    How to iterate through Object ArrayList?

    Hello! I have the below code and want to add to, then iterate through the array list whenever a user presses the register button. I've tried .get but it prints junk like this onto the screen: UserData@7f0c871e

    Code:
    	public class RegButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			if(!userBox.getText().equals("") && !passBox.getText().equals(""))
    			{
    				UserData user = new UserData();
    				user.CreateUser(userBox.getText(), passBox.getText(), 5000);
    				userInfo.add(user);
    			}
    		}
    	}
    Then, here is the UserData class:

    Code:
    class UserData
    {
    	String name;
    	String pass;
    	int ID;
    	
    	public void CreateUser(String name, String pass, int ID)
    	{
    		this.name = name;
    		this.pass = pass;
    		this.ID = ID;
    	}
    }
    Any easier or better way to do this?

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: How to iterate through Object ArrayList?

    it prints junk like this onto the screen: UserData@7f0c871e
    That is the String returned by the default toString() method for the UserData class. If you want to see something different,
    override the toString() method and have it return the String you want to see.

    better way to do this?
    Pass the args to the UserData class's constructor instead of having a special method that sets the class's values.
    Norm

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured