Sergii Lisnychyi
Sergii Lisnychyi

Reputation: 507

Populate array with null values

Maybe that's simple question. But, I wonder to know why we can't populate array or collection with null values? Please look such simple example:

public static void main(String[] args) {

   Map<String, Object> map = new HashMap<>();
   map.put("first",null);
   map.put("first1",new BigDecimal(1.5));
   map.put("first2",new BigDecimal(2.5));
   map.put("first3",new BigDecimal(3.5));


   String[]array1 = new String[map.values().size()];
   Object[]array2 = new Object[map.values().size()];

   int counter = 0;

   for(Map.Entry<String,Object> entry: map.entrySet()){

       String header = entry.getKey();
       Object value = entry.getValue();

       array1[counter] = header;
       array2[counter] = value;

       counter++;

   }
}

I would be glad to listen your purposes.

Upvotes: 2

Views: 15741

Answers (4)

blalasaadri
blalasaadri

Reputation: 6218

Add the following code under your for-loop and you'll see that not only are the sizes equal to that of the map, the elements are the same too.

System.out.println("counter = " + counter);
System.out.println("array1.length = " + array1.length);
for(int i=0; i<array1.length; i++) {
   System.out.printf("- array1[%d] = %s\n", i, array1[i]);
}
System.out.println("array2.length = " + array2.length);
for(int i=0; i<array2.length; i++) {
   System.out.printf("- array2[%d] = %s\n", i, array2[i]);
}

The order will probably not be the same order you entered the values in but that's due to the nature of the HashMap. My output was:

counter = 4
array1.length = 4
- array1[0] = first3
- array1[1] = first2
- array1[2] = first
- array1[3] = first1
array2.length = 4
- array2[0] = 3.5
- array2[1] = 2.5
- array2[2] = null
- array2[3] = 1.5

Upvotes: 0

sreisman
sreisman

Reputation: 668

When using generics, I think the best practice is to specify something more specific than Object, in order to be more typesafe. So you may want to update the HashMap to:

Map<String, BigDecimal> map = new HashMap<Sting, BigDecimal>();

Then you can create null BigDecimal objects to populate your map.

Upvotes: 0

linski
linski

Reputation: 5094

You can use Arrays.fill to fill an array with values, e.g.:

String[] arr = new String[5]; 
System.out.println(Arrays.deepToString(arr));
Arrays.fill(arr,"initial value");
System.out.println(Arrays.deepToString(arr));
Arrays.fill(arr,null);
System.out.println(Arrays.deepToString(arr));

outputs:

[null, null, null, null, null]
[initial value, initial value, initial value, initial value, initial value]
[null, null, null, null, null]

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236150

An object array is populated with null values when it's instantiated. Collections on the other hand are empty at the beginning, so there' nothing that can be "populated" in the first place - well, you could fill them with null values if you wanted, but what would be the point of that? only add elements as needed to a Collection, it makes no sense to fill it with null values (and not all collections will allow it, it depends on the type).

Upvotes: 4

Related Questions