Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 2395 of 3366
998 Views
In this article, we will learn how to parse for words in a string for a specific word in Java, i.e., we need to check if a specific word exists in a string, and if it does, we will parse the string to find that word. There are various methods in Java that you can use to parse a string for a specific word. Here we are going to discuss 3 of them. Using the contains() method Using the indexOf() method Using the StringTokenizer class Using ... Read More
2K+ Views
How to create a variable that can be set only once, meaning it can be assigned a value only once but is not final in Java? Solution In Java we can solve the above using the following two methods: Using a custom wrapper class Using an AtomicReference Using a custom wrapper class We can create a custom wrapper class that allows setting a value only once. The class will have a boolean flag to check if the value has already been set. Wrapper classes are used for converting primitive data types into objects. We will use this for setting ... Read More
1K+ Views
Once you declare a variable final, after initializing it, you cannot modify its value further. Moreover, like instance variables, final variables will not be initialized with default values.Therefore, it is mandatory to initialize final variables once you declare them. If not a compile time error will be generated.Example Live Demopublic class FinalExample { final int j; public static void main(String args[]){ FinalExample obj = new FinalExample(); System.out.println(obj.j); } }Compile time errorFinalExample.java:5: error: non-static variable j cannot be referenced from a static context System.out.println(j); ^ 1 errorInitializing the final variableYou can initialize a ... Read More
5K+ Views
Problem Statement The task is, given a string and a substring, you have to check if a substring is present in a string or not. Do not mind if the case of the string and substring is different, it should not matter. For example, if the string is "Hello World" and the substring is "hello", then the output should be true. Even if the substring is "HELLO" or "HeLLo", the output should be true. Solution To solve this problem, we will first convert the string and the substring to lowercase using the toLowerCase() method. The toLowerCase () method of the ... Read More
16K+ Views
ArrayList is a part of the Java Collections Framework. Which is a dynamic type of array that can grow and shrink as needed. It is a resizable array implementation of the List interface. The ArrayList class is used when we want to store a list of elements in a dynamic array. In this article, let's learn how to get the first and last elements from an ArrayList in Java. The get() method of the ArrayList class accepts an integer representing the index value and returns the element of the current ArrayList object at the specified index. Therefore, if you pass ... Read More
2K+ Views
In this article, let's learn how to remove a sublist from an ArrayList in Java. The ArrayList class is a part of the Java Collections Framework. It is a resizable array and an implementation of the List interface. The ArrayList class is used when we want to store a list of elements in a dynamic array. Sub-list is a specific portion of an array list. Basically, it is a list of elements that are part of the original list but it is not the entire list itself. Using subList() and clear() method. ... Read More
442 Views
Using copyOf() methodThe copyOf() method of the Arrays class (java.util package) accepts two parameters −an array (of any type).an integer value representing length.And copies the contents of the given array from starting position to given length and returns the new array.Example Live Demoimport java.util.Arrays; public class CopyingSectionOfArray { public static void main(String[] args) { String str[] = new String[10]; //Populating the array str[0] = "Java"; str[1] = "WebGL"; str[2] = "OpenCV"; str[3] = "OpenNLP"; str[4] = "JOGL"; ... Read More
5K+ Views
Problem Statement The given problem is to validate a string to check if that string contains only alphabets (both uppercase and lowercase) and does not contains any other characters like numbers, special characters, etc.. For example, the string "HelloWorld" is valid as it contains only alphabets, while "Hello123" is not valid as it contains numbers. Solution We can solve this problem by using following methods: Using Regular Expressions Manual Checking using loop Using Character.isAlphabetic() Using Regular Expressions We can use regular expressions to validate a string for alphabets. The regular expression ^[a-zA-Z]+$ matches a string that contains only ... Read More
4K+ Views
Problem Statement Here we have a string that contains integer values, our task is to sort those integer values in ascending order. For example, if the string is "31134", the sorted string should be "11334". Solution We can solve the above problem using multiple ways: Using char array Using Stream API Using Bubble Sort Using char array We will use the toCharArray() method of the String class to convert the string into a character array. Then we will sort the character array using the Arrays.sort() method. ... Read More
12K+ Views
The Boolean class of the lang package provides two method namely parseBoolean() and valueOf().parseBoolean(String s) − This method accepts a String variable and returns boolean. If the given string value is "true" (irrespective of its case) this method returns true else, if it is null or, false or, any other value it returns false.valueOf(String s) − This method accepts a String value, parses it and returns an object of the Boolean class based on the given value. You can use this method instead of the constructor. If the given String value is "true" this method returns true or, it returns ... Read More