- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4338 Articles for Java 8
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
477 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
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]
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
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
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
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
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
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
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