To force PHP to use strings for array keys is quite easy, as in PHP array keys are automatically stored as integers if the array elements are numbers. If they are not numbers then it will cast to strings. Forcing PHP to Use strings for Array Keys Following are the ways to force PHP to use strings for array Keys Using php array() function Using json_encode() function Using php array() Function If you use the array() function and keep the first key in quotes it will take other keys as a string does not matter if you are ... Read More
A queue is a linear data structure where elements are stored in the FIFO manner. Here, FIFO stands for First In First Out which means the first element inserted would be the first element to be accessed. In this article we will see how to perform different queue operations like Enqueue and Dequeue using Java programming language. Queue in Java In Java, the Queue interface, a sub interface of Java Collection Interface, provides the features of queue data structure. Since it is an interface, the Queue interface needs a concrete class for the implementation and the most common classes are ... Read More
LinkedHashSet is a class provided by Java that implements Set Interface. The first element of a LinkedhashSet is nothing but the first element in the collection. In this article, we will discuss different approaches to get the first element from LinkedHashset. What is a LinkedHashSet? A LinkedHashSet is a collection which is a combination of HashSet collection and a LinkedList collection. A HashSet is an unordered collection which doesn't allow duplicates. But, a LinkedList is an ordered collection which stores duplicates Coming to LinkedHashSet as it is a combination of both HashSet and LinkedList it stores the elements in ... Read More
A stack is a linear data structure where elements are stored in the LIFO manner. Here, LIFO stands for Last In First Out which means the last element inserted would be the first element to be accessed. In Java, stack is a class provided by the Java collection framework and it implements the Stack data structure. In this article we will write Java program to find if an element is in stack or not. Using Stack.search() Method The method java.util.Stack.search() is used to find if an element is in the stack or not in Java. This method takes a ... Read More
The problem statement states that we have given a String str of length N (where N is an integer) containing alphanumeric characters. We need to recursively remove all adjacent duplicate characters so that the resultant string does not contain any adjacent duplicate characters. We can use a recursive or iterative approach to solve the problem. Here, we first remove the adjacent duplicate elements from the left part of the string. After that, we recursively remove the adjacent duplicates from the right part of the string. Example Scenario 1: Input: str1 = "tuttor"; Output: res = tuor The adjacent duplicate ... Read More
The Calendar class of java.util package provides a method with the name add(). This method accepts current date and amount of time as parameter values. If the given amount of time is positive, it will add it to the current date, and in the case of negative time, it will subtract it. In this article, we will see a few Java programs to add year to the current date using the Calendar.add() method. Example Scenario: Input 1: current_date = Thu Nov 22 18:19:06 UTC 2018 Input 2: year_to_add = 12 Output: new_date = Thu Nov 22 18:19:06 UTC 2030 ... Read More
Both StringTokenizer class and the split() method in Java are used to divide a string into tokens or substrings. However, they are different from each other. The StringTokenizer class does not support regular expressions, whereas the split() method works with regular expressions. In this article, we will see some Java examples that show how to split strings using them. Example Scenario: Input: str = "simple easy learning" Output: split_str = "simple", "easy", "learning" Splitting String using StringTokenizer Class The StringTokenizer is a legacy class of java.util package. This class provides methods to break a string into multiple tokens. It ... Read More
The atoi() function is used in C programming language and used to convert the string which is passed as the parameter to it into an integer value if the string is a valid integer otherwise it shows the undefined behavior. We will implement the atoi() function in the Java programming language. Example Scenario 1: Input: string str = "123" Output: res = 123 We are given a string that represents a number so we have just got the same output. Example Scenario 2: Input: string str = "897c7" Output: res = Invalid Input The given string ... Read More
In this problem, we are given a String and our task is to find all palindromic sub-strings of the specified length. There are two ways to solve the problem. The first way is to compare the characters of sub-string from start to last, and another way is to reverse the sub-string and compare it with the original sub-string to check whether it is palindrome. String in Java is a class which represents a sequence of characters. It is immutable which means once it is created a String object cannot be changed. And, sub-string is a small portion or part of ... Read More
On clearing the StringBuffer object, all the characters from the buffer will be removed. In this article, we will write Java program to clear the StringBuffer. StringBuffer is a peer class of String that provides much of the functionality of strings. But, String represents fixed-length, immutable character sequences while StringBuffer represents mutable character sequences. Example Scenario: Input: obj = Java Program Output: res = The result will be an empty StringBuffer object. Using delete() method The StringBuffer class of java.lang package provides a method named delete() to clear the StringBuffer. This method accepts start and end indices ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP