xFlare xFlare - 1 year ago 66
Java Question

How can I assign a object an item in java

For example in PhP I would do something like this:

public $hashedpasswords = [];

$this->hashedpasswords[$object->getId()] = $this->getHashedPasscode($object);

How can I make a array like this in Java?

Answer Source

First things first. Java is a different language from PHP. Things you need to know regarding to this question is,

  1. Java is a object oriented language
  2. Java is strictly typed language

strict typed language means you need to define the type of a object when you initializing. Then you can only assign values in the defined type. ie:

int array[]; //this array expects only integer elements

Therefore, you can't use Java arrays on this purpose.

object oriented languages allows you to define a object (classes in java) and use their instances to work with object's items. Therefore, you don't need to add them into a array. You can directly define an instance of the so called object. ie:

hashedpassword hp;

hp = new hashedpassword(); //assign the declared object or declare the object here

If your requirement is to add map key value pairs, you can use maps in Java or you can manually define a keyvaluepair class as in this example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download