0

I need to create an array with an undefined size, which will contain user information.

For example:

user[0]["name"] = "Patrick";

However, the standard java array seems to require a known length, which I don't know.

What alternative can i use? (I'll gladly see some coding examples as well) I'm using a TCP framework () which doesn't allow to pass objects with constructors. Therefore, as far as i can see, making a User object is not possible.

Cameron Fredman
  • 1,259
  • 11
  • 24
Patrick Reck
  • 303
  • 1
  • 10
  • 24

4 Answers4

3

Firstly you should use Objects instead of arbitary data structures and if you don't know the size, I would use a List

List<Person> people = new ArrayList<Person>();
people.add(new Person("Patrick"));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • The problem is, my Player class have a constructor, which the TCP framework i'm using (Kryonet) doesn't allow to be sent – Patrick Reck Dec 03 '12 at 10:55
  • You can't send any object, even a String object over TCP. You have to serialize/encode it in some manner. I would define your class as it should and serialize it as a seperate consideration. – Peter Lawrey Dec 03 '12 at 11:04
  • I ment the frameworks serializer can't handle objects with constructors :-) – Patrick Reck Dec 03 '12 at 11:05
  • 1
    There are ways to fix this in the framework (I assume this is not an option), but usually frameworks only require you have a public constructor, not that it be your only constructor. – Peter Lawrey Dec 03 '12 at 11:09
1

In java we can only use integer values for cell indexes.

I guess you're looking for a list of maps:

List<Map<String, String>> persons = new ArrayList<Map<String, String>>();

// add Patrick
Map<String, String> person = new HashMap<String, String>();
person.put("name", "Patrick");
person.put("age", "23");
persons.add(person);

// add Sue
person = new HashMap<String, String>();
person.put("name", "Sue");
person.put("age", "21");
persons.add(person);

To access all names, for instance:

for (Map<String, String> person:persons) {
  System.out.println(person.get("name"));
}
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • This is definitely what i need. However, i can't add multiple persons, it just overwrites the others. How do i add multiple persons to this? :-) – Patrick Reck Dec 03 '12 at 11:17
  • Each person "is a" map in the list. Simply create a new map for each new person and add it to the list (persons). I've added Patrick in the last line. – Andreas Dolk Dec 03 '12 at 11:19
  • I can't get it working. Could you possibly expand your example to include a second person? – Patrick Reck Dec 03 '12 at 11:20
0

you cannot use associative arrays in java. In other words, you cannot use "name" as a key.

But you can create empty arrays without specifying size by doing:

String[] array = new String[]{};

However, attempting to put something into this array will give you an indexoutofbound exception

Runar Halse
  • 3,528
  • 10
  • 39
  • 59
0

You should consider create a class (like Person) and use only a List to manage it. The you could do something like this:

List<Peson> myLyst = new ArrayList<Person>();
Person person = new Person("Patrick");
myList.add(0, person);
Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40