Initializing a boolean array in java with an example program

  • Initializing a boolean variable : boolean b=true;
  • In some cases we need to initialize all values of boolean array with true or false.
  • In such cases we can use Arrays.fill() method
  • Arrays.fill(array, Boolean.FALSE);
  • java initialize boolean array with true:  Arrays.fill(array, Boolean.FALSE);
  • Lets see an example java program on how to assign or initialize boolean array with false or true values.



#1: Java Example program on initializing boolean array.

  1. package com.instanceofjava;

  2. import java.util.Arrays;
  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview questions
  6.  * 
  7.  * Description: Initialize boolean array values with false or true
  8.  *
  9.  */
  10. public class InitializeBoolean {

  11. public static void main(String[] args) {
  12. Boolean[] array = new Boolean[4];
  13. //initially all values will be null
  14. for (int i = 0; i < array.length; i++) {
  15. System.out.println(array[i]);
  16. }
  17. Arrays.fill(array, Boolean.FALSE);
  18. // all values will be false
  19. for (int i = 0; i < array.length; i++) {
  20. System.out.println(array[i]);
  21. }
  22. Arrays.fill(array, Boolean.TRUE);
  23. // all values will be false
  24. for (int i = 0; i < array.length; i++) {
  25. System.out.println(array[i]);
  26. }
  27. }

  28. }

Output:

  1. null
  2. null
  3. null
  4. null
  5. false
  6. false
  7. false
  8. false
  9. true
  10. true
  11. true
  12. true


java initialize boolean array with true

How to convert array to string without brackets in java

  • Converting array to string and string should not contain any brackets.
  • We can convert array to string by iterating array and capturing each element from an array and append to StringBuilder/StringBuffer so that final output will be string without brackets.
  • Lets see an example java program to convert array to string by removing brackets.
  • So that we can remove brackets from string.



#1 : Java Example program to convert array to string without brackets.

  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: convert array to string without brackets.
  7.  *
  8.  */
  9. public class ArrayToString {

  10. public static void main(String[] args) {
  11.  String array[]= {"java","string","without","brackets"};
  12.  StringBuffer strbuffer = new StringBuffer();
  13.  for (String str : array) {
  14.   strbuffer.append(str).append(" ");
  15.  }
  16.  String result = strbuffer.toString();
  17.  System.out.println(result);
  18.  
  19. }

  20. }

Output:

  1. java string without brackets 



convert array to string without brackets

Java Program to convert ArrayList to String array

  • Java code to convert arraylist to string array.
  • We can convert ArrayList of strings to String array by using  toArray() method.
  • Lets see an Example java program to convert ArrayList to string array.



#1:  Java example program to convert ArrayList to String Array

  1. package com.instanceofjava;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. public class ArrayListToStringArray {
  5. /**
  6. * @author www.Instanceofjava.com
  7. * @category interview programs
  8. * Description: Java Prorgam to convert ArrayList to String array
  9. *
  10. */
  11. public static void main(String[] args) {
  12. List<String> lstflowers = new ArrayList<String>();
  13. lstflowers.add("Rose");
  14. lstflowers.add("Lilly");

  15. String[] arrayflower = new String[lstflowers.size()];
  16. arrayflower = lstflowers.toArray(arrayflower);

  17. for(String flower : arrayflower)
  18.     System.out.println(flower);
  19. }

  20. }


Output:


  1. Rose
  2. Lilly

#2:  Java example program to convert ArrayList to String Array Using java 8

  1. package com.instanceofjava;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. public class ArrayListToStringArray {
  5. /**
  6. * @author www.Instanceofjava.com
  7. * @category interview programs
  8. * Description: Java Prorgam to convert ArrayList to String array using java 8
  9. *
  10. */
  11. public static void main(String[] args) {
  12. List<String> lstflowers = new ArrayList<String>();
  13. lstflowers.add("Rose");
  14. lstflowers.add("Lilly");

  15. String[] arrayflower = lstflowers.toArray(new String[lstflowers.size()]);

  16. for(String flower : arrayflower)
  17.     System.out.println(flower);
  18. }

  19. }


java program to convert arraylist to string array

5 Different ways to print arrays in java

  •  Arrays in java are used to hold similar data types values.
  • System.out.print() method does not print values of array.
  • In order to print array values we have different ways.
  • Normally we use for loop and by using index we will print each element inside array.
  • let us see how many ways we can able to print values of array. 


1.Print array in java using for loop
  • How to print array in java using for loop?
  • Yes we can print arrays elements using for loop.
  • Find the length of the array using array.length and take initial value as 0 and repeat until  array.length-1. 
  • Then access each index values of an array then print.
#1. Write a program to print array in java using for loop

  1. package arraysinterview;
  2.  
  3. public class PrintArray {
  4.  
  5.     /**
  6.      * How to print java array using for loop
  7.      * @author www.instanceofjava.com
  8.      */
  9.     
  10. public static void main(String [] args){
  11.         
  12.         int[] array = { 12,13,8,34,2,7,9,43,54,21};
  13.         
  14.         for (int i = 0; i < array.length; i++) {
  15.             System.out.println(array[i]);
  16.         }
  17.         
  18. }
  19.  
  20. }

Output:

  1. 12
  2. 13
  3. 8
  4. 34
  5. 2
  6. 7
  7. 9
  8. 43
  9. 54
  10. 21

2. Print string array in java using Enhanced for loop

  • From java 1.5 using enhanced for loop also we can print array values.

#2.   How to print string array in java using for each loop



3. Using Arrays.toString(array) method
  • By  using Arrays.toString(array) method we can print array values.

#3 Write a program to print array in java using Arrays.toString(array)

  1. package arraysinterview;
  2. import java.util.Arrays;
  3.  
  4. public class PrintArray {
  5.     /**
  6.      * How to print java array using Arrays.toString(array)
  7.      * @author www.instanceofjava.com     */
  8.  
  9.   public static void main(String [] args){
  10.         
  11.         String[] array = { "hi", "hello", "java"};
  12.         
  13.         
  14.    System.out.println(Arrays.toString(array));
  15.                
  16.   }
  17. }

Output:

  1. [hi, hello, java]
4.Using Arrays.deepToString(array) Method

  • Arrays,deepToString(array) method added in java 5 with generics and varargs.

#4 Write a program to print array in java using  Arrays.deepToString(array)

  1. package arraysinterview;
  2. import java.util.Arrays;
  3.  
  4. public class PrintArray {
  5.     /**
  6.      * How to print java array using Arrays.deepToString(array)
  7.      * @author www.instanceofjava.com     */
  8.  
  9.   public static void main(String [] args){
  10.         
  11.         int[][] array = new int[][]{
  12.                 {1,2,3},
  13.                 {11,12,13},
  14.                 {4 ,5,6},
  15.                 };
  16.         
  17.   System.out.println(Arrays.deepToString(array));
  18.       
  19.                
  20.   }
  21. }

Output:
  1. [[1, 2, 3], [11, 12, 13], [4, 5, 6]]

5.Using Arrays.asList(array) Method

  • Arrays,asList(array) method we can print array elements

#5 Write a program to print array in java using  Arrays.asList(array)

  1. package arraysinterview;
  2. import java.util.Arrays;
  3.  
  4. public class PrintArray {
  5.     /**
  6.      * How to print java array using Arrays.asList(array)
  7.      * @author www.instanceofjava.com     */
  8.  
  9.   public static void main(String [] args){
  10.         
  11.       int[] array ={1,2,3,4,5,6};        
  12.      System.out.println(Arrays.asList(array));
  13.  
  14.      String[] strarray ={"hi","array","print"};
  15.       System.out.println(Arrays.asList(strarray));
  16.                
  17.   }
  18. }

Output:
  1. [[I@2a139a55]
  2. [hi, array, print]

Find Second smallest number in java without sorting

Java interview Program to find second smallest number in an integer array without sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. int[] x ={10,11,12,13,14,6,3,-1};
  5.  
  6.         int small=x[0];
  7.  
  8.  for(int i=0;i<x.length;i++)
  9.  {
  10.         if(x[i]<small)
  11.         {
  12.         small=x[i];
  13.         }
  14.  }
  15.  
  16.    int sec_Small=x[0];
  17.  
  18. for(int i=0;i<x.length;i++)
  19.  {
  20.         if(x[i]<sec_Small && x[i]!=small)
  21.         {
  22.         sec_Small=x[i];
  23.         }
  24.   }
  25.  
  26.         System.out.println("Second Smallest Number: "sec_Small);
  27.         }
  28. }



Output:
 
  1. Second Smallest Number:3

Java interview  Program to find second Smallest number in an integer array by sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8.   Arrays.sort(numbers);
  9.  
  10.   System.out.println("Smallest Number: "+numbers[0]);
  11.   System.out.println("Second Smallest Number: "+numbers[1]);

  12.  }
  13.  
  14. }




Output:
 
  1. Smallest Number: 3
  2. Second Smallest Number: 5



Find second highest number in an integer Array

Java Interview Program to find second highest number in an integer array without sorting the elements.


  1. package com.instanceofjava;
  2. class SecondLargestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7. int highest = 0;
  8.  int second_highest = 0;
  9.  
  10. for(int n:numbers){
  11.  
  12. if(highest < n){
  13.  
  14.       second_highest = highest;
  15.       highest =n;
  16.  
  17.  } else if(second_highest < n){
  18.  
  19.                 second_highest = n;
  20.  
  21. }
  22.  
  23. }
  24.         System.out.println("First Max Number: "+highest);
  25.         System.out.println("Second Max Number: "+second_highest);

  26.  
  27.  }
  28.  
  29. }



Output:
 
  1. First Max Number: 64
  2. Second Max Number: 46

Java Interview Program to find second highest number in an integer array by sorting the elements.


  1. package com.instanceofjava;
  2. class SecondLargestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8.   Arrays.sort(numbers);
  9.  
  10.   System.out.println("Largest Number: "+numbers[numbers.length-1]);
  11.   System.out.println("Second Largest Number: "+numbers[numbers.length-2]);
  12.  }
  13.  
  14. }




Output:
 
  1. Largest Number: 64
  2. Second Largest Number: 46



Select Menu