Found 4338 Articles for Java 8

How to create and populate two-dimension Java array?

Sai Subramanyam
Updated on 16-Jun-2020 09:53:19

651 Views

A two-dimensional array in Java is represented as an array of one-dimensional arrays of the same type. Mostly, it is used to represent a table of values with rows and columns −Example Live Demopublic class Creating2DArray { public static void main(String args[]) { int[][] myArray = new int[3][3]; myArray[0][0] = 21; myArray[0][1] = 22; myArray[0][2] = 23; myArray[1][0] = 24; myArray[1][1] = 25; myArray[1][2] = 26; myArray[2][0] = 27; myArray[2][1] = 28; myArray[2][2] = 29; for(int i=0; i

How to create and populate Java array of Hash tables?

Lakshmi Srinivas
Updated on 16-Jun-2020 09:52:15

475 Views

One way to create an array of hash tables is to create Hashtable objects and to assign these to a Hashtable array using curly braces:ExampleLive Demoimport java.util.Hashtable; public class HashTableOfArrays {    public static void main(String args[]) {       Hashtable ht1 = new Hashtable();       ht1.put("Name", "Krishna");       ht1.put("Age", "28");       ht1.put("City", "Visakhapatnam");       ht1.put("Phone", "9848022338");             Hashtable ht2 = new Hashtable();       ht2.put("Name", "Raju");       ht2.put("Age", "30");       ht2.put("City", "Chennai");       ht2.put("Phone", "9848033228");     ... Read More

How to Convert a Java 8 Stream to an Array?

karthikeya Boyini
Updated on 19-Dec-2019 08:54:20

288 Views

To convert a stream to an Array in Java -Collect the stream to a list using the Collect interface and the Collectors class.Now convert the list to an array using the toArray() method.ExampleLive Demoimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class J8StreamToArray {    public static void main(String args[]) {       String[] myArray = { "JavaFX", "OpenCV", "WebGL", "HBase" };       Stream stream = Stream.of(myArray);       List list = stream.collect(Collectors.toList());       String[] str = list.toArray(new String[0]);       System.out.println(Arrays.toString(str));    } }Output[JavaFX, OpenCV, WebGL, HBase]

How to read data from scanner to an array in java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:20

10K+ Views

The Scanner class of the java.util package gives you methods like nextInt(), nextByte(), nextFloat() etc. to read data from keyboard. To read an element of an array uses these methods in a for loop:Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class ReadingWithScanner {    public static void main(String args[]) {       Scanner s = new Scanner(System.in);       System.out.println("Enter the length of the array:");       int length = s.nextInt();       int [] myArray = new int[length];       System.out.println("Enter the elements of the array:");       for(int i=0; i

How to convert Java Array to Iterable?

V Jyothi
Updated on 16-Jun-2020 08:52:32

8K+ Views

To make an array iterable either you need to convert it to a stream or as a list using the asList() or stream() methods respectively. Then you can get an iterator for these objects using the iterator() method.ExampleLive Demoimport java.util.Arrays; import java.util.Iterator; public class ArrayToIterable {    public static void main(String args[]){       Integer[] myArray = {897, 56, 78, 90, 12, 123, 75};       Iterator iterator = Arrays.stream(myArray).iterator();       while(iterator.hasNext()) {          System.out.println(iterator.next());       }    } }Output897 56 78 90 12 123 75As mentioned above, you can also convert ... Read More

How to handle Java Array Index Out of Bounds Exception?

Sravani S
Updated on 19-Feb-2020 10:49:26

14K+ Views

Generally, an array is of fixed size and each element is accessed using the indices. For example, we have created an array with size 9. Then the valid expressions to access the elements of this array will be a[0] to a[8] (length-1).Whenever you used an –ve value or, the value greater than or equal to the size of the array, then the ArrayIndexOutOfBoundsException is thrown.For Example, if you execute the following code, it displays the elements in the array asks you to give the index to select an element. Since the size of the array is 7, the valid index ... Read More

How to write contents of a file to byte array in Java?

Sharon Christine
Updated on 19-Dec-2019 08:53:10

739 Views

The FileInputStream class contains a method read(), this method accepts a byte array as a parameter and it reads the data of the file input stream to given byte array.Exampleimport java.io.File; import java.io.FileInputStream; public class FileToByteArray {    public static void main(String args[]) throws Exception {       File file = new File("HelloWorld");       FileInputStream fis = new FileInputStream(file);       byte[] bytesArray = new byte[(int)file.length()];       fis.read(bytesArray);       String s = new String(bytesArray);       System.out.println(s);    } }Output//Class declaration public class SampleProgram {    /* This is my ... Read More

How to store the contents of arrays in a file using Java?

Ramu Prasad
Updated on 16-Jun-2020 08:56:30

2K+ Views

You can use write data into a file using the Writer classes. In the example given below, we are writing the contents of the array using the BufferedWriter.ExampleLive Demoimport java.io.BufferedWriter; import java.io.FileWriter; public class WritingStringArrayToFile {    public static void main(String args[]) throws Exception {       String[] myArray = {"JavaFX", "HBase", "OpenCV", "Java", "Hadoop", "Neo4j"};       BufferedWriter writer = new BufferedWriter(new FileWriter("myFile.txt", false));       for(int i = 0; i < myArray.length; i++) {          writer.write(myArray[i].toString());          writer.newLine();       }       writer.flush();     ... Read More

If a number is read as a String, how to Count the no of times the highest number repeated in the string? a. Ex: 2 4 3 4 2 4 0 -> (3)

Govinda Sai
Updated on 16-Jun-2020 08:59:00

61 Views

To do so, Trim the given string and split it using the split() method (Removing the empty spaces in-between).Create an integer array and in loop convert each element of the string array to an integer using Integer.parseInt() method and assign it to the respective element of the integer array.Sort the obtained integer array, since this method sorts the elements of the array in ascending order the last element will be the maximum of the array. Create an integer variable count with initial value 0. Compare each element of the array with the maximum value, each time a match occurs increment the count. The final value ... Read More

How to cast a list of strings to a string array?

Swarali Sree
Updated on 30-Jul-2019 22:30:20

313 Views

The java.util.ArrayList.toArray() method returns an array containing all of the elements in this list in proper sequence (from first to last element).This acts as bridge between array-based and collection-based APIs. You can convert a list to array using this method of the List class − Example Live Demo import java.util.ArrayList; import java.util.List; public class ListOfStringsToStringArray { public static void main(String args[]) { List list = new ArrayList(); list.add("JavaFX"); list.add("HBase"); ... Read More

Advertisements