-1

I was trying to generate a dynamic object names that will change everytime an object is created. Something like:

first object name would be like "userName" and the following would be like "userName1".

I'm new to java, and I tried to initialize int count=0 and count++ to do it, but the User class doesn't seem to allow me to do that like userName+count. Therefore, is there anyway i could possible go about doing it?

I have search through all the similar thread about dynamic object name, but it doesn't seems to work out for my case.

EDIT

So i have this app built up, that allow user to create account. And, now i encounter this problem where whenever i tried to create new account,

Firebase clear away all my previous data that i have inserted, and replace it with the current inserted data.

Here's my code:

Main.java

name = (EditText) findViewById(R.id.edit_rName);
email = (EditText) findViewById(R.id.edit_rEmail);
password = (EditText) findViewById(R.id.edit_rPassword);

Button btnSubmit = (Button) findViewById(R.id.btn_rSubmit);
    btnSubmit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

Firebase f = new Firebase("https://xxx.firebaseio.com/");

        Firebase usersRef = f.child("users");
        Map<String, User> users = new HashMap<String, User>();

        users.put(name.getText().toString(), new User(name.getText().toString(), password
                .getText().toString(), email.getText().toString()));

        usersRef.setValue(users);
    }
    });

User.java

public class User {
    private String fullName;
    private String password;
    private String email;

    public User(String fullName, String password,String email) {
        this.fullName = fullName;
        this.password = password;
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public String getFullName() {
        return fullName;
    }
}
Minorsee
  • 67
  • 10
  • do you mean full name or userName object that you created using `User userName = new User();` – SMA Feb 09 '15 at 16:03
  • 1
    Short answer: you can't. – m0skit0 Feb 09 '15 at 16:04
  • 1
    why do you want to give object name dynamically? What is the purpose? – aadi53 Feb 09 '15 at 16:05
  • The purpose that i need the object name to be dynamically is because i need the object name to be different every time user create an account, The reason is that im using FirebaseAPI and Firebase API, setValue() method only takes different object name or else it will replace the whole data in the database – Minorsee Feb 10 '15 at 02:10

1 Answers1

2

You should use an array if you know in advance how many objects you will create, and a list if you don't know in advance.

Array:

String[] usernames = new String[4];
usernames[0] = "Alex";
usernames[1] = "Bob";
usernames[2] = "Carol";
usernames[3] = "David";
//usernames[4] = "Eliza"; // won't work, out of bounds!
System.out.println(usernames[2]); //prints Carol

List(recommended) (first import java.util.ArrayList):

List<String> usernames = new ArrayList<String>();
usernames.add("Alex");
usernames.add("Bob");
usernames.add("Carol");
usernames.add("David");
usernames.add("Eliza");
System.out.println(usernames.get(2)); // prints Carol
usernames.set(2, "Carlos");
System.out.println(usernames.get(2)); // prints Carlos
nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • In Java he really should use a List in both cases. – m0skit0 Feb 09 '15 at 16:04
  • 1
    @m0skit0 Can you outline the reasons for why a beginner would want to use a list over an array? The array would return null for elements not set, and except beyond bounds, while the list would except beyond a moving bound. – nanofarad Feb 09 '15 at 16:05
  • Make sure to code oriented to interface, not to class implementation. – Luiggi Mendoza Feb 09 '15 at 16:06
  • Sure: easier manipulation, no size constraints, list can also have null elements, has a wide API for most things you will want to do with a list, can walk through with `for each` operator... Resuming: you will need to code much less (sometimes way much less) if you use a list. – m0skit0 Feb 09 '15 at 16:09
  • Another good reason for a beginner to use Lists is precisely to learn Java, which is not a minor consideration. – m0skit0 Feb 09 '15 at 16:11
  • This method doesn't seems to be working for my case, I have edit my question,. could you possibly take a look on it? i think my question wasnt clear previously – Minorsee Feb 10 '15 at 03:12