How do I set the size of a list in Java?

Aishwarya Naglot
Updated on 10-Jun-2025 15:08:24

9K+ Views

Java list size is dynamic. It increases automatically whenever you add an element to it, and this exceeds the initial capacity. You can define the initial capacity at the time of list creation so that it allocates memory after the initial capacity is exhausted. We can do this using the ArrayList Constructor and Collections.nCopies() method. In this article, we will explore both methods to set the size of a list in Java. Using ArrayList Constructor Using Collections.nCopies() Let's explore these methods in detail. Using ArrayList Constructor We can create an ... Read More

What is the best way to read an entire file into a std::string in C++?

Revathi Satya Kondra
Updated on 10-Jun-2025 15:02:33

2K+ Views

To read an entire file into a std::string in C++, you can open the file using std::ifstream, read its contents using a std::stringstream or by moving the file pointer at the specified position, and then store the result in a std::string. Algorithm Here is a simple algorithm to read an entire file into a std::string in C++: Begin. Open the file using an ifstream object. Check if the file is successfully opened. Create an ostringstream object. Read the file content using rdbuf() and write it into the ostringstream. ... Read More

Java program to display time in different country’s format

Aishwarya Naglot
Updated on 10-Jun-2025 14:47:26

699 Views

In this article, we will learn how to display the current date in different country formats using Java. We can import java.time package to work with the date and time API. The java.time package, along with the DateFormat and Locale classes, allows us to format the date according to various regions' standards. Displaying Date in Different Country Formats There are two or more ways to display the date in different country formats in Java: Using DateFormat Class Using LocalDate Class and DateTimeFormatter Let's explore each of these methods in detail. Using ... Read More

Java Program to Display Dates of Calendar Year in Different Format

Aishwarya Naglot
Updated on 10-Jun-2025 14:43:48

416 Views

In this article, we will understand how to display dates of a calendar year in different formats. Java has a built-in Date class, but it is recommended to use the java.time package, to work with the modern date and time API. The package includes many date and time classes. Following are the ways to display dates of a calendar year in different formats: Using DateFormat Class Using SimpleDateFormat Class Using LocalDate Class and DateTimeFormatter Using DateFormat Class The DateFormat class is part of the java.text package. ... Read More

Builtin functions of GCC compiler in C++

Revathi Satya Kondra
Updated on 10-Jun-2025 14:33:31

773 Views

When you want to write a program in C++, your compiler (like GCC) converts your code into computer language. While doing this, GCC offers some special functions called built-in functions. The built-in functions are predefined functions by the compiler itself, but not provided by any standard library. The GCC compiler provides several built-in functions. Some of these functions are listed below: __builtin_popcount(x) __builtin_parity(x) __builtin_clz(x) __builtin_ctz(x) The __builtin_popcount(x) Function This builtin function is used to count the number of 1s in an integer ... Read More

How does “void *” differ in C and C++?

Revathi Satya Kondra
Updated on 10-Jun-2025 14:27:29

632 Views

Both languages (C and C++) support void pointers, but their behaviour is different. In C, a void pointer can be directly assigned to any other pointer type without the need for a typecast. However, in C++, assigning a void pointer to any other pointe type require an explicit typecast. In this article, we will learn the differences between void pointers in C and C++.. Void Pointer in C A void pointer (also called a generic pointer) in C is a special type of pointer that can point to any data type, but doesn’t have any type by itself. It ... Read More

Java Program to Iterate over ArrayList using Lambda Expression

Aishwarya Naglot
Updated on 10-Jun-2025 14:26:28

3K+ Views

The ArrayList class is a resizable array that can be found in java.util package. The difference between a built-in array and an ArrayList in Java is that the size of an array cannot be modified. Lambda Expression is a feature that was introduced in Java 8. It is useful when we use collections, such as ArrayList, Set, or Map, and we want to perform operations on the elements of these collections. In this article, we will learn how to iterate over an ArrayList using a lambda expression in Java. Iterating Over an ArrayList Using Lambda Expressions There are two main ... Read More

Wide char and library functions in C++

Revathi Satya Kondra
Updated on 10-Jun-2025 13:24:03

4K+ Views

Wide Characters Wide characters are similar to character datatype. The main difference is that char takes 1-byte space, but wide character takes 2-bytes (sometimes 4-byte depending on compiler) of space in memory. For 2-byte space wide character can hold 64K (65536) different characters. So the wide char can hold UNICODE characters. The UNICODE values are international standard which allows for encoding for characters virtually for any character of any language. Example 1: Size of a single wide character This program demonstrates how to declare a single wide character using wchar_t to print its value and memory size. #include using namespace ... Read More

Return from void functions in C++

Revathi Satya Kondra
Updated on 10-Jun-2025 13:16:07

18K+ Views

The void functions are called void because they do not return anything. "A void function cannot return anything" this statement is not always true. From a void function, we cannot return any values, but we can return something other than values. Some of them are like below. A void function can return A void function cannot return any values. But we can use the return statement. It indicates that the function is terminated.ExampleThe following example demonstrates a void function with the return statement: #include using namespace std; void my_func() { cout

Exception handling and object destruction in C++

Revathi Satya Kondra
Updated on 10-Jun-2025 13:11:44

894 Views

In this article, you will learn what is exception handling, object destruction, and Handing exception thrown in Object Destructor in C++. C++ Exception Handling An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw. try: This block contains the code ... Read More

Advertisements