-2

I'm beginner in Java environment, accustomed to PHP.

And in Php we can create an array like the code below :

$array['params1'] = 'the first param';
$array['params2'] = 'the second param';

And when i will output $array['params1'] it will be 'the first param'.

But i do not find any similar solutions in Java, do you know something similar ?

Thanks in advance

Eklavya
  • 15,459
  • 4
  • 14
  • 41
user11000657
  • 146
  • 8
  • 4
    Firstly, do some research yourself. That being said.... associative arrays are called Maps in Java. This overview of the Collections Framework is for an old version of Java but it should help you get up to speed: https://docs.oracle.com/javase/tutorial/collections/ and https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html –  Jun 13 '20 at 10:20
  • Does this answer your question? [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – Ebenezer Isaac Jun 13 '20 at 15:50

1 Answers1

3

As @mrblewog said, you might want to readup on data structures and the syntax in Java as it is quite different than php.

To give you an Example:

//      Key     Value
HashMap<String, String> map = new HashMap<>();
map.insert("key1", "value1");
map.get("key1"); // returns "value1"

If you want to store other objects than Strings you will need to change the generic types (written in the <X, Y>).

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Flo
  • 131
  • 1
  • 6
  • Yes i know, but i'm searching in Array instead of Collection.. Thanks a lot for your reply, it is exactly what i need :) – user11000657 Jun 13 '20 at 10:35