Programming Articles - Page 1432 of 3363

Can we define an enum inside a class in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:34:06

17K+ Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntaxenum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.ExampleLive Demopublic class EnumerationExample {    enum Enum {       Mango, Banana, Orange, Grapes, Thursday, Apple    }    public static void main(String args[]) {       Enum constants[] = Enum.values();       System.out.println("Value of constants: ");         for(Enum d: constants) {         ... Read More

How to search a directory with file extensions in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:21:27

1K+ Views

Following example prints the files in a directory based on the extensions −Exampleimport java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) throws IOException {       Stream path = Files.walk(Paths.get("D:\ExampleDirectory"));       System.out.println("List of PDF files:");       path = path.filter(var -> var.toString().endsWith(".pdf"));       path.forEach(System.out::println);             path = Files.walk(Paths.get("D:\ExampleDirectory"));       System.out.println("List of JPG files:");       path = path.filter(var -> var.toString().endsWith(".jpg"));       path.forEach(System.out::println);           path = Files.walk(Paths.get("D:\ExampleDirectory"));       ... Read More

How to list the hidden files in a directory in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:21:14

952 Views

The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the file names passed on the passed filter.To get the hidden directories in a folder, implement a FileFilter which accepts only hidden directories, and pass it as a parameter to the listFiles() method.Exampleimport java.io.File; import java.io.FileFilter; import java.io.IOException; public class Test{    public static void main(String args[]) throws IOException {   ... Read More

How to handle IllegalArgumentException inside an if using Java

Maruthi Krishna
Updated on 08-Feb-2021 11:30:06

639 Views

While you use the methods that causes IllegalArgumentException, since you know the legal arguments of them, you can restrict/validate the arguments using if-condition before-hand and avoid the exception.We can restrict the argument value of a method using the if statement. For example, if a method accepts values of certain range, you can verify the range of the argument using the if statement before executing the method.ExampleFollowing example handles the IllegalArgumentException caused by the setPriority() method using the if statement.Live Demoimport java.util.Scanner; public class IllegalArgumentExample {    public static void main(String args[]) {       Thread thread = new Thread(); ... Read More

How to get the names of the empty directories in a directory in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:20:45

516 Views

The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the file names passed on the passed filter.To get the directories in a folder implement a FileFilter which accepts only empty directories, and pass it as a parameter to the listFiles() method.Exampleimport java.io.File; import java.io.FileFilter; import java.io.IOException; public class MyExample{    public static void main(String args[]) throws IOException {     ... Read More

How to create object arrays Java? What are their disadvantages

Maruthi Krishna
Updated on 08-Feb-2021 11:15:48

212 Views

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.Element: Each item stored in an array is called an element.Index: Each location of an element in an array has a numerical index, which is used to identify the element.Creating object arraysYes, since objects are also considered as datatypes (reference) in Java, you can create an array of the type of a particular class and, populate ... Read More

How to get the directories (only) from a folder using Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:15:36

1K+ Views

The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the file names passed on the passed filter.To get the directories in a folder implement a FileFilter which accepts only directories and pass it as a parameter to the listFiles() method.Following is a screen shot of the contents of the ExampleDirectoryExampleimport java.io.File; import java.io.FileFilter; import java.io.IOException; public class MyExample{    public ... Read More

How to multiply matrices elements if matrices are stored in a list in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:16:14

547 Views

To multiply matrices elements if matrices are stored in a list, we can make use of Reduce function. For example, if we have four matrices named as M1, M2, M3, and M4 stored in a list object called List then the multiplication each element in all the four matrices can be done by using the command Reduce("*",List).Example1 Live DemoM1

How to set the plot area to assign the plots manually in base R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 13:38:43

120 Views

We can split the screen to assign the plots manually in the plot area. The split.screen function can be used for this purpose. For example, if we want to create 4 plots in the plot window then we can use split.screen(c(2, 2)). Now to create the plot in first screen the command will be screen(1) then plot will be created. If we again create the plot then the original plot in screen(1) will be replaced with the new one. To create the plot in 3rd screen that is screen(3), we first need to use the command screen(3) and then create ... Read More

How to increase the X-axis length of histogram in base R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:11:14

6K+ Views

To increase the X-axis length of histogram in base R, we can use the axis function. The most important thing to remember while using the axis function is mentioning the appropriate sequence based on the values in the vector or in the column of the data frame if a data frame is used.Example Live Demox

Advertisements