1
String loginInfo[][] = { { "mason", "dragon"   }, //First Column is usernames, second is passwords.
                         { "shay",  "stowers"  }, 
                         { "admin", "password" }, };

This is my array that has the username in the first col, passwords in the second col. I want to be able to add another row to this array once a user has submitted two strings.

Example...

String input1 = "username", String input2 = "password";

I want to take these two Strings and add them as a row, to the array. So my end result would look like this..

String loginInfo[][] = { { "mason",    "dragon"   },
                         { "shay",     "stowers"  },
                         { "admin",    "password" },
                         { "username", "password" }, };//This coming from input1 and input2
3
  • 1
    You can't... arrays are of fixed length. you'd better take a look at the java.util.Collections Framework. Commented Apr 5, 2014 at 4:57
  • Arrays are fixed-size, so you can't add to the original one. You need to make a new array that's at least one size bigger. A much better solution would be to use an List<List<String>>. Commented Apr 5, 2014 at 4:58
  • Take a look at Map in the Collections API. Commented Apr 5, 2014 at 4:58

2 Answers 2

1

Perfect place to use an ArrayList of your user information JavaBean like

List<UserInfo> userInfo = new ArrayList<UserInfo>();

Whenever you receive userName and passWord, create an instance of UserInfo and add it to the list

UserInfo Class like below

class UserInfo{

 private String userName = null;

 private String passWord = null;

 // add a constructor
 public UserInfo(String userName, String passWord){
  this.userName = userName;
  this.passWord = passWord; 
 }

 //add setters and getters
 ...
 ...

}

Check it out. You will like it

Disclaimer : Not tested. Just a prototype!

Sign up to request clarification or add additional context in comments.

4 Comments

Perhaps u could give a simple structure of the UserInfo class to the OP.
Well done... too bad I can offer only one upvote. :-)
Thanks, we haven't yet delved into multiple classes yet so I think I'll just stick to 2 lists for now, one for usernames and one for passwords.
No, Please create an object (Javabean) to store both username and password collectively and have a single list. This will keep your data organised.
1

Arrays are not changeable. They are the length they are originally given. You have two options:

  1. Create a new array, increasing its length, then copy the old array into the new one.
  2. Use a collection in which Java has already done all this for you. Check out Lists.

EDIT: Example for option 1:

string[ ][ ] newloginInfo = new int[loginInfo.length * 2][2];

for (int i = 0; i < loginInfo.length; i++) {
    System.arraycopy(loginInfo[i], 0, newloginInfo[i], 0, loginInfo[0].length);
}

//now there is room for twice as many rows.  Add as usual.

Example for option 2: simply use the Generic list instead of the array to being with:

List<List<of string>> list = new ArrayList<List<of String>();  

then you just call list.add to add in a new array:

List<of string> newrow = new ArrayList<of string>();
list.add(newrow);

apologies - not tested or thrown in an ide - check the syntax.

EDIT 2: Octopus's suggestion of having an object and then storing a list of those objects is really the way to go instead of dealing with multidimensional arrays - unless you have a REALLY good reason not you use it. It's cleaner, more organized, and your IDE will pick up the details in intellisense.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.