Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 62 of 151

ByteBuffer asFloatBuffer() method in Java

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

A view of the ByteBuffer can be created as a FloatBuffer using the asFloatBuffer() method in the class java.nio.ByteBuffer. This method requires no parameters and it returns a float buffer as required. This buffer reflects the changes made to the original buffer 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) {       int n = 50;       try {          ByteBuffer bufferB = ByteBuffer.allocate(n);          FloatBuffer bufferF = bufferB.asFloatBuffer();         ...

Read More

How to extract multiple integers from a String in Java?

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

Let’s say the following is our string with integer and characters −String str = "(29, 12; 29, ) (45, 67; 78, 80)";Now, to extract integers, we will be using the following pattern −\dWe have set it with Pattern class −Matcher matcher = Pattern.compile("\d+").matcher(str);Exampleimport java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String[] args) {       String str = "(29, 12; 29, ) (45, 67; 78, 80)";       Matcher matcher = Pattern.compile("\d+").matcher(str);       Listlist = new ArrayList();       while(matcher.find()) {          list.add(Integer.parseInt(matcher.group()));       }       System.out.println("Integers = "+list);    } }OutputIntegers = [29, 12, 29, 45, 67, 78, 80]

Read More

Getting a subvector from a vector in C++

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

This is a C++ program for getting a subvector from a vector in C++AlgorithmBegin   Declare s as vector s(vector const &v, int m, int n) to    initialize start and end position of vector to constructor.       auto first = v.begin() + m.         auto last = v.begin() + n + 1.       Declare a variable vector of vector type.          Pass the value of first and last position of vector.       Return vector.    Declare a template T.    Declare a function show().       ...

Read More

Java Program to change a file attribute to writable

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

Let’s say our file is “input.txt”, which is set read-only −File myFile = new File("input.txt"); myFile.createNewFile(); myFile.setReadOnly();Now, set the above file to writable −myFile.setWritable(true);After that, you can use canWrite() to check whether the file is writable or not.Exampleimport java.io.File; public class Demo {    public static void main(String[] args) throws Exception {       File myFile = new File("input.txt");       myFile.createNewFile();       myFile.setReadOnly();       if (myFile.canWrite()) {          System.out.println("Writable!");       } else {          System.out.println("Read only mode!");       }       // ...

Read More

CharBuffer array() method in Java

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

A char array for the buffer can be obtained using the method array() in the class java.nio.CharBuffer. If the returned array is modified, then the contents of the buffer are also similarly modified and vice versa. If the buffer is read-only, then the ReadOnlyBufferException is thrown.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 buffer = CharBuffer.allocate(n);          buffer.put('A');          buffer.put('P');   ...

Read More

How to create a Queue from LinkedList in Java?

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

Let us create a queue from LinkedList like this −Queuequeue = new LinkedList(); queue.add("P"); queue.add("Q"); queue.add("R"); queue.add("S"); queue.add("T"); queue.add("U"); queue.add("V");Now, use a List to display the elements of the queue −Listlist = new ArrayList(queue);Exampleimport java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Demo {    public static void main(String[] args) {       Queuequeue = new LinkedList();       queue.add("P");       queue.add("Q");       queue.add("R");       queue.add("S");       queue.add("T");       queue.add("U");       queue.add("V");       Listlist = new ArrayList(queue);       for (Object ob: ...

Read More

How to use subSet() method of Java NavigableSet Class

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

Use the subset() method to get elements from a limit. At first, create NavigableSet and add elements −NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85);Now, use the subset() method −set.subSet(40, 85)The following is an example to implement subset() method of Java NaviagbleSet class −Exampleimport java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100); System.out.println("Returned Value = " + set.subSet(40, 85)); } }OutputReturned Value = [40, 55, 70]

Read More

Java Program to create a new list with values from existing list with Function Mapper

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

To create a new list with values from fields from existing list with Function Mapper, the following is an example. Here, we have a class Employee −class Employee {    private String emp_name;    private int emp_age;    private String emp_zone; }The following is an example that creates a new list from existing with Function Mapper −Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; public class Demo {    public static void main(String args[]) {       List < Employee > emp = Arrays.asList(new Employee("Jack", 29, "South"), new Employee("Tom", 24, "North"),          new Employee("Harry", 35, "West"), ...

Read More

How to write a switch statement in a JSP page?

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

Following is the example of using a switch statement within a JSP page.           SWITCH...CASE Example                   The above code will generate the following result −It's Wednesday.

Read More

AbstractSequentialList in Java

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

The Java Collection Framework contains the AbstractSequentialList class. This class is used to create and implement an unmodifiable list. Also this class implements the Collection interface and the AbstractCollection class.A program that demonstrates this is given as follows −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       AbstractSequentialList list = new LinkedList();       list.add("John");       list.add("Martha");       list.add("Sally");       list.add("Susan");       list.add("Harry");       System.out.println("The elements are: " + list);    } }The output of the above program is as follows −OutputThe ...

Read More
Showing 611–620 of 1,507 articles
« Prev 1 60 61 62 63 64 151 Next »
Advertisements