Found 7442 Articles for Java

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

119 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

431 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

How to count Java comments of a program that is stored in a text file?

Samual Sam
Updated on 16-Jun-2020 08:49:25

535 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

How to check a String for palindrome using arrays in java?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:20

9K+ 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

How to delete elements from an array?

Sai Subramanyam
Updated on 16-Jun-2020 08:45:29

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

How to reverse the elements of an array using stack in java?

Ramu Prasad
Updated on 16-Jun-2020 11:41:10

4K+ 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

How to add elements to the midpoint of an array in Java?

Lakshmi Srinivas
Updated on 19-Dec-2019 08:51:41

247 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]

How to create an array of linked lists in java?

Abhinanda Shri
Updated on 16-Jun-2020 09:02:50

5K+ Views

A linked list is a sequence of data structures, which are connected together via links.To create an array of linked lists, create required linked lists and, create an array of objects with them.ExampleLive Demoimport java.util.LinkedList; public class ArrayOfLinkedList {    public static void main(String args[]) {       LinkedList list1 = new LinkedList();       list1.add("JavaFX");       list1.add("Hbase");             LinkedList list2 = new LinkedList();       list2.add("OpenCV");       list2.add("Mahout");             LinkedList list3 = new LinkedList();       list3.add("WebGL");       list3.add("CoffeeScript");       Object[] obj = {list1, list2, list3};             for (int i=0; i

How to find the Odd and Even numbers in an Array in java?

Govinda Sai
Updated on 19-Feb-2020 10:54:29

3K+ Views

In the loop check, the result of i%2 operation on each element if 0 the element is even else the element is odd.ExampleLive Demopublic class OddNumbersInAnArray {    public static void main(String args[]) {       int[] myArray = {23, 93, 56, 92, 39};       System.out.println("Even numbers in the given array are:: ");       for (int i=0; i

How to print data of specific element from an array in java?

Monica Mona
Updated on 30-Jul-2019 22:30:20

14K+ Views

An array is a data structure/container/object that stores a fixed-size sequential collection of elements of the same type. The size/length of the array is determined at the time of creation. The position of the elements in the array is called as index or subscript. The first element of the array is stored at the index 0 and, the second element is at the index 1 and so on. Each element in an array is accessed using an expression which contains the name of the array followed by the index of the required element in square brackets. You can access an ... Read More

Advertisements