Found 33676 Articles for Programming

Can a final class be subclassed in Java?

Alshifa Hasnain
Updated on 27-Feb-2025 19:28:35

2K+ Views

In Java Inheritance is a fundamental feature that allows a class to derive properties and behaviors from another class. In Java, not all classes can be subclassed. A final class is a special type of class that cannot be extended.  What is a Final Class in Java? In Java, a class is declared final using the final keyword. The final modifier for finalizing the implementations of classes, methods, and variables. When a class is marked as final, it means − It cannot be extended. All its methods remain unchanged in their ... Read More

How do I get the parent directory in Python?

Niharikaa Aitam
Updated on 29-Apr-2025 19:11:23

3K+ Views

In Python, when working with files and directories, we may often need to access the parent directory of a given path, especially when going through a file system or managing relative paths in a project. In this article, we are going to explore the different methods available in Python to get the parent directory of the current or any specific path. Using os Module One of the commonly used methods to interact with the file system in Python is using the os module. This module provides various utilities to work with file paths and directories. To get the parent directory, ... Read More

How to convert JSON Array to normal Java Array?

Ramu Prasad
Updated on 19-Feb-2020 12:24:47

3K+ Views

The get method of the JSONArray class returns the element at a particular index. Using this method, you can get the elements of the JSONArray object and populate the array with them.Exampleimport java.util.Arrays; import org.json.JSONArray; public class JsonToArray {    public static void main(String args[]) throws Exception {       String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"};       JSONArray jsArray = new JSONArray();       for (int i = 0; i < myArray.length; i++) {          jsArray.put(myArray[i]);      }      System.out.println(jsArray);      String[] array = new String[myArray.length];   ... Read More

How to convert Java Array/Collection to JSON array?

Sravani S
Updated on 30-Jul-2019 22:30:20

8K+ Views

Google provides a library named org.json.JSONArray and, following is the maven dependency to add library to your project. com.googlecode.json-simple json-simple 1.1 The JSONArray class of the org.json package provides put() method. Using this method, you can populate the JSONArray object with the contents of the elements.Exampleimport org.json.JSONArray; public class ArrayToJson { public static void main(String args[]) { String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"}; JSONArray jsArray = new JSONArray(); ... Read More

How to perform binary search on an array in java?

V Jyothi
Updated on 16-Jun-2020 10:17:11

199 Views

The Arrays class of java package provides you a method named binarySearch() using this method you can perform a binary search on an array in Java.ExampleLive Demoimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int intArr[] = {30,20,5,12,55};       Arrays.sort(intArr);       System.out.println("The sorted int array is:");       for (int number : intArr) {          System.out.println("Number = " + number);       }       int searchVal = 12;       int retVal = Arrays.binarySearch(intArr,searchVal);       System.out.println("The index of element 12 is : " + retVal);    } }OutputThe sorted int array is: Number = 5 Number = 12 Number = 20 Number = 30 Number = 55 The index of element 12 is: 1

How to perform heapsort on an array in Java?

Priya Pallavi
Updated on 19-Feb-2020 12:22:49

679 Views

Following is the algorithm for heapsort (maxheap).Step 1 − Create a new node at the end of the heap.Step 2 − Assign new value to the node.Step 3 − Compare the value of this child node with its parent.Step 4 − If the value of parent is less than a child, then swap them.Step 5 − Repeat step 3 & 4 until Heap property holds.Exampleimport java.util.Arrays; import java.util.Scanner; public class Heapsort {    public static void heapSort(int[] myArray, int length) {       int temp;       int size = length-1;       for (int i ... Read More

How to check if array contains three consecutive dates in java?

Nikitha N
Updated on 19-Feb-2020 12:22:03

1K+ Views

To check to find whether a given array contains three consecutive dates:Convert the given array into a list of type LocalDate.Using the methods of the LocalDate class compare ith, i+1th and i+1th, i+2th elements of the list if equal the list contain 3 consecutive elements.ExampleLive Demoimport java.time.LocalDate; import java.time.Month; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; public class ConsicutiveDate {    public static void main(String args[]) {       String[] dates = {"5/12/2017", "6/12/2017", "7/12/2017"};       List localDateList = new ArrayList();       for (int i = 0; i

How to get a list of all sub-directories in the current directory using Python?

Niharikaa Aitam
Updated on 29-Apr-2025 19:21:37

13K+ Views

In Python, when working with files and directories, we may often need to get a list of subdirectories within a specific location, especially the current working directory. In this article, we are going to use the different methods available in Python to get a list of all subdirectories in the current directory. Using os Module While working with files, one of the most widely used approaches is using os module. This module provides a way to interact with the operating system to perform tasks such as navigating directories, reading file structures, and more. In this module, we have a ... Read More

What is the lambda expression to convert array/List of String to array/List of Integers in java?

Srinivas Gorla
Updated on 16-Jun-2020 10:12:36

831 Views

You can convert an array list of strings to an array list of integers as:ExampleLive Demoimport java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class LamdaExpressions {    public static void main(String args[]) {       ArrayList list = new ArrayList();       list.add("123");       list.add("223");       list.add("323");       list.add("334");       List listInteger =          list.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());       System.out.println(listInteger);    } }Output[123, 223, 323, 334]

How can I put a Java arrays inside an array?

Abhinanda Shri
Updated on 16-Jun-2020 10:11:04

2K+ Views

ExampleLive Demoimport java.util.Arrays; public class ArrayWithinAnArray{    public static void main(String args[]) {       int[] myArray1 = {23, 56, 78, 91};       int[] myArray2 = {123, 156, 178, 191};       int[] myArray3 = {223, 256, 278, 291};       int[] myArray4 = {323, 356, 378, 391};       int [][] arrayOfArrays = {myArray1, myArray2, myArray3, myArray4};       System.out.println(Arrays.deepToString(arrayOfArrays));    } }Output[[23, 56, 78, 91], [123, 156, 178, 191], [223, 256, 278, 291], [323, 356, 378, 391]]

Advertisements