Programming Articles

Page 1505 of 2547

CharBuffer wrap() method in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 341 Views

A character array can be wrapped into a buffer using the method wrap() in the class java.nio.CharBuffer. This method requires a single parameter i.e. the char array to be wrapped into a buffer and it returns the new buffer created. If the returned buffer is modified, then the contents of the array are also similarly modified and vice versa.A program that demonstrates this is given as follows −Exampleimport java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { try { ...

Read More

How to initialize a vector in C++?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

Initialization vector can be done in many ways1) Initialize a vector by push_back() methodAlgorithmBegin    Declare v of vector type.    Call push_back() function to insert values into vector v.    Print “Vector elements:”.    for (int a : v)       print all the elements of variable a. End.Example#include #include using namespace std; int main() {    vector v;    v.push_back(6);    v.push_back(7);    v.push_back(10);    v.push_back(12);    cout

Read More

Delete file or directory on termination in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 339 Views

A file or directory can be deleted on termination of the program i.e. after the virtual machine terminates using the method java.io.File.deleteOnExit(). This method requires no parameters and it does not return a value.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          System.out.println("File: " + file);          file.deleteOnExit();       } catch(Exception e) {          e.printStackTrace();   ...

Read More

Custom ArrayList in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

A custom ArrayList can have multiple types of data and its attributes in general are based on the user requirements.A program that demonstrates a custom ArrayList is given as follows −Exampleimport java.util.ArrayList; public class CustomArrayList {    int n = 5;    class Employee {       int eno;       String name;       Employee(int eno, String name) {          this.eno = eno;          this.name = name;       }    }    public static void main(String args[]) {       int eno[] = {101, 102, 103, 104, ...

Read More

How to create Java Priority Queue to ignore duplicates?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

The simplest way to create a Java Priority Queue to ignore duplicates is to first create a Set implementation −HashSet set = new HashSet (); set.add(100); set.add(150); set.add(250); set.add(300); set.add(250); set.add(500); set.add(600); set.add(500); set.add(900);Now, create Priority Queue and include the set to remove duplicates in the above set −PriorityQueuequeue = new PriorityQueue(set);Exampleimport java.util.HashSet; import java.util.PriorityQueue; public class Demo {    public static void main(String[] args) {       HashSetset = new HashSet();       set.add(100);       set.add(150);       set.add(250);       set.add(300);       set.add(250);       set.add(500);   ...

Read More

What are the different types of nested classes are defined in Java?

raja
raja
Updated on 11-Mar-2026 1K+ Views

In Java, it is possible to define a class inside another class, such classes are called Nested classes. We can use the access modifiers like private, public, protected or default for inner classes and default or public access modifiers for outer class.There are two types of nested classes are defined in Java.Static Nested ClassNon-Static Nested ClassStatic Nested ClassWe Can define an inner class as static, so such type of classes is called a static nested class.The nested class is defined with the static keyword, so this type of nested classes doesn’t share any relationship with the instance of an outer class.A static ...

Read More

How to fetch elements with iterator in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 358 Views

Let us first create a List and add elements −Listlist = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" });Now, use Iterator to fetch each element −Iteratori = list.iterator();Display the elements −while (i.hasNext()) {    System.out.println(i.next()); }Exampleimport java.util.Arrays; import java.util.Iterator; import java.util.List; public class Demo {    public static void main(String[] a) {       Listlist = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" });       Iteratori = list.iterator();       System.out.println("Displaying elements...");       while (i.hasNext()) {          System.out.println(i.next());       }   ...

Read More

ArrayBlockingQueue Class in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 237 Views

A bounded blocking queue that is backed by an array is known as a ArrayBlockingQueue Class in Java. The size of the queue is fixed in the class and it uses FIFO for ordering elements. The ArrayBlockingQueue Class is a member of the Java Collections Framework.A program that demonstrates this is given as follows −Exampleimport java.util.concurrent.ArrayBlockingQueue; public class Demo {    public static void main(String[] args) {       int n = 10;       ArrayBlockingQueue abQueue = new ArrayBlockingQueue(n);       abQueue.add(7);       abQueue.add(2);       abQueue.add(6);       abQueue.add(3);     ...

Read More

CharBuffer slice() method in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 198 Views

A new CharBuffer with the content as a shared subsequence of the original CharBuffer can be created using the method slice() in the class java.nio.CharBuffer. This method returns the new CharBuffer that is read-only if the original buffer is read-only and direct if the original buffer is direct.A program that demonstrates this is given as follows −Exampleimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 5;       try {          CharBuffer buffer1 = CharBuffer.allocate(n);          buffer1.put('A');         ...

Read More

How to shuffle a std::vector in C++

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

A vector shuffle can be done in the Fisher-Yates shuffle algorithm.In this algorithm, a linear scan of a vector is done and then swap each element with a random element among all the remaining element, including the element itself.AlgorithmBegin   Declare a function show().       Pass a constructor of a vector as a parameter within show() function.       for (auto const& i: input)          Print the value of variable i.       Declare v of vector type.          Initialize some values into v vector in array pattern.     ...

Read More
Showing 15041–15050 of 25,466 articles
Advertisements