
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to fill (initialize at once) an array in Java



Problem Description:
How to fill (initialize at once) an array?
Solution:
This example fill (initialize all the elements of the array in one short) an array by using Array.fill(arrayname,value) method and Array.fill(arrayname ,starting index ,ending index ,value) method of Java Util class.
import java.util.*; public class FillTest { public static void main(String args[]) { int array[] = new int[6]; Arrays.fill(array, 100); for (int i=0, n=array.length; i |
Result:
The above code sample will produce the following result.
100 100 100 100 100 100 100 100 100 50 50 50 |



Advertisements |
Advertisements