![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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 9326 Articles for Object Oriented Programming
![Sravani S](https://www.tutorialspoint.com/assets/profiles/13538/profile/60_67753-1512711355.jpg)
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
![Sharon Christine](https://www.tutorialspoint.com/assets/profiles/13517/profile/60_82215-1512649681.jpg)
725 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
![Ramu Prasad](https://www.tutorialspoint.com/assets/profiles/13539/profile/60_87996-1512650215.jpg)
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
![Govinda Sai](https://www.tutorialspoint.com/assets/profiles/13542/profile/60_86497-1512650655.jpg)
60 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
![Swarali Sree](https://www.tutorialspoint.com/assets/profiles/13515/profile/60_84115-1512649510.jpg)
311 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
357 Views
You can read the contents of a file using the Scanner class and You can find the comments in a particular line using contains() method.Exampleimport java.io.*; import java.util.Scanner; public class FindingComments { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(new File("HelloWorld")); String input; int single = 0; int multiLine = 0; while (sc.hasNextLine()) { input = sc.nextLine(); if (input.contains("/*")) { multiLine ++; ... Read More
![Ankitha Reddy](https://www.tutorialspoint.com/assets/profiles/13550/profile/60_73963-1512711511.jpg)
7K+ Views
To verify whether the given string is a palindrome (using arrays) Convert the given string into a character array using the toCharArray() method. Make a copy of this array. Reverse the array. Compare the original array and the reversed array. in case of match given string is a palindrome. Example import java.util.Arrays; import java.util.Scanner; public class Palindrome { public static void main(String args[]) { System.out.println("Enter a string "); Scanner sc = new Scanner(System.in); String s ... Read More
![Sai Subramanyam](https://www.tutorialspoint.com/assets/profiles/13512/profile/60_68191-1512710774.jpg)
1K+ Views
To delete an element at a particular position from an array. Starting from the required position, replace the element in the current position with the element in the next position.Example Live Demopublic class DeletingElementsBySwapping { public static void main(String args[]) { int [] myArray = {23, 93, 56, 92, 39}; System.out.println("hello"); int size = myArray.length; int pos = 2; for (int i = pos; i
![Ramu Prasad](https://www.tutorialspoint.com/assets/profiles/13539/profile/60_87996-1512650215.jpg)
3K+ Views
Stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.A stack is first in first out, it has two main operations push and pop. Push inserts data in to it and pop retrieves data from it.To reverse an array using stack initially push all elements in to the stack using the push() method then, retrieve them back using the pop() method into another array.ExampleLive Demoimport java.util.Arrays; import java.util.Stack; public class ReversinArrayUsingStack { ... Read More
![Lakshmi Srinivas](https://www.tutorialspoint.com/assets/profiles/13528/profile/60_91717-1512651120.jpg)
169 Views
Apache commons provides a library named org.apache.commons.lang3 and, following is the maven dependency to add library to your project. org.apache.commons commons-lang3 3.0 This package provides a class named ArrayUtils. You can add an element at a particular position in an array using the add() method of this class.Exampleimport java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class AddingElementToMidPoint { public static void main(String args[]) { int[] myArray = {23, 93, 30, 56, 92, 39}; int eleToAdd = 40; int position= myArray.length/2; int [] result = ArrayUtils.add(myArray, position, eleToAdd); System.out.println(Arrays.toString(result)); } }Output[23, 93, 30, 40, 56, 92, 39]