Sunday 5 January 2020

How to convert boolean[] to Boolean[] in Java?


By traversing through each element of array we can convert Boolean[] to Boolean[].

App.java
package com.sample.app;

public class App {

 private static void printElements(Boolean[] arr) {
  for (Boolean i : arr) {
   System.out.print(i + " ");
  }
  System.out.println();
 }

 public static Boolean[] getBoxedArray(boolean[] arr) {
  if (arr == null)
   return null;

  Boolean[] result = new Boolean[arr.length];
  int index = 0;
  for (boolean i : arr) {
   result[index] = Boolean.valueOf(i);
   index++;
  }
  return result;
 }

 public static void main(String args[]) {
  boolean[] arr = { true, false, true, false };

  Boolean[] result1 = getBoxedArray(arr);

  printElements(result1);

 }

}

Output
true false true false


You may like

No comments:

Post a Comment