Found 34488 Articles for Programming

How to find the index of given element of a Java List?

Govinda Sai
Updated on 30-Jul-2019 22:30:21

15K+ Views

The indexOf(Object) method of the java.util.ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Using this method, you can find the index of a given element.Example Live Demoimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add("G");       arrlist.add("E");       arrlist.add("F");       arrlist.add("M");       System.out.println("Size of list: " + arrlist.size());       for (String value : arrlist) {       ... Read More

How to convert a comma separated String into an ArrayList in Java?

Sravani S
Updated on 30-Jul-2019 22:30:21

8K+ Views

To convert a comma separated String into an ArrayListSplit the String into an array of Strings using the split() method.Now, convert the obtained String array to list using the asList() method of the Arrays class.Example Live Demoimport java.util.Arrays; import java.util.List; public class Sample {    public static void main(String[] args) {       String myString = "JavaFx,Java,WebGL,OpenCV";       String[] myArray = myString.split(",");       System.out.println("Contents of the array ::"+Arrays.toString(myArray));       List myList = Arrays.asList(myArray);    } }OutputContents of the array ::[JavaFx, Java, WebGL, OpenCV]

How to replace an element of an ArrayList in Java?

Priya Pallavi
Updated on 30-Jul-2019 22:30:21

8K+ Views

You can replace an element of an ArrayList using the set() method of the Collections class. This method accepts two parameters an integer parameter indicating the index of the element to be replaced and an element to replace with.Example Live Demoimport java.util.ArrayList; public class ArrayListSample {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       System.out.println(list);       list.set(2, "HBase");       System.out.println(list);    } }Output[JavaFx, Java, WebGL, OpenCV] [JavaFx, Java, HBase, OpenCV]

How to sort an ArrayList in Java in descending order?

V Jyothi
Updated on 30-Jul-2019 22:30:21

6K+ Views

To sort the contents of an ArrayList in descending orderCreate an ArrayList.Sort the contents of the ArrayList using the sort() method of the Collections class.Then, reverse array list using the reverse() method of the Collections class.Example:import java.util.ArrayList; import java.util.Collections; public class ArrayListSample {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       Collections.sort(list);       System.out.println(list);       Collections.reverse(list);       System.out.println(list);    } }Output:[Java, JavaFx, OpenCV, WebGL] [WebGL, OpenCV, JavaFx, Java]

How to sort an ArrayList in Java in ascending order?

Priya Pallavi
Updated on 30-Jul-2019 22:30:21

441 Views

You can sort an ArrayList using the sort() method of the Collections class this method accepts a list object as a parameter and sorts the contents of it in ascending order.Example:import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class ArrayListSample {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       Set set = new LinkedHashSet(list);       Collections.sort(list);       System.out.println(list);    } }Output:[Java, JavaFx, OpenCV, WebGL]

How to synchronize an ArrayList in Java?

Nikitha N
Updated on 30-Jul-2019 22:30:21

165 Views

The synchronizedList(List list) method of the Collections class accepts a List object and returns a synchronized list backed by the specified list.Example:import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class ArrayListSample {    public static void main(String[] args){       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       Set set = new LinkedHashSet(list);       Collections.synchronizedList(list);       System.out.println(list);    } }Output:[JavaFx, Java, WebGL, OpenCV]

How to match any non-digit character in Python using Regular Expression?

Pranav Indukuri
Updated on 08-Nov-2022 11:38:59

5K+ Views

A regular expression is a group of characters that allows you to use a search pattern to find a string or a set of strings. RegEx is another name for regular expressions. The re module in Python is used to work with regular expressions. In this article we look how to extract non digit characters in python by using regular expression. We use \D+ regular expression in python to get non digit characters from a string. Where, \D returns a match that does not contain digits. + implies zero or more occurrences of characters. Using findall() function In ... Read More

How to reverse an ArrayList in Java?

Srinivas Gorla
Updated on 30-Jul-2019 22:30:21

312 Views

The Collections class provides a method named reverse() this method accepts a list and reverses the order of elements in it.Example:import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class Sample {    public static void main(String[] args){       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       Set set = new LinkedHashSet(list);       System.out.println(set);       Collections.reverse(list);       System.out.println(list);    } }Output:[JavaFx, Java, WebGL, OpenCV] [OpenCV, WebGL, Java, JavaFx]

How to match any one uppercase character in python using Regular Expression?

Pranav Indukuri
Updated on 08-Nov-2022 11:22:45

1K+ Views

There are two ways to match any one uppercase character in python using Regular Expression. One is the general method and other is using the regular expression. Using Regular Expression A regular expression is a series of characters that allows you to discover a string or a set of strings using a search pattern. Regular expressions are sometimes known as RegEx. In Python, the re module is used to work with regular expressions. In this article we will match the uppercase vowel in python using regular expression. To achieve this, we use r'[A, E, I, O, U]' regular expression. Here, ... Read More

How to match any one lowercase vowel in python using Regular Expression?

Pranav Indukuri
Updated on 08-Nov-2022 11:19:11

669 Views

There are two ways to match any one lowercase vowel in python using Regular Expression. One is the general method and other is using the regular expression. Using the general method In the following code, we have matched all the lowercase vowels from the string 'TutRoIals POinT Is A GreAt PlaTfOrm to lEarN '. Here, we did not use regular expression to match. We used the general method to match the lowercase vowels. Example The following example is a program which shows the matching of any one lowercase vowel in python using the general method. string='TutRoIals POinT Is A GreAt ... Read More

Advertisements