Found 267 Articles for Java8

Java program to implement insertion sort

karthikeya Boyini
Updated on 13-Mar-2020 05:49:39

689 Views

This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be inserted in this sorted sub-list has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort.The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array).Algorithm1.If it is the first element, it is already sorted. return 1; 2.Pick next element 3.Compare with all elements in the sorted sub-list ... Read More

Java program to implement selection sort

Arjun Thakur
Updated on 13-Mar-2020 05:44:07

3K+ Views

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary from one element to the right.Algorithm1.Set MIN to location 0 2.Search the minimum element in the ... Read More

Java program to print the factorial of the given number

Ankith Reddy
Updated on 13-Mar-2020 05:36:50

4K+ Views

Factorial of a positive integer n is the product of all values from n to 1. For example, the factorial of 3 is (3 * 2 * 1 = 6). Algorithm1. Take integer variable A 2. Assign a value to the variable 3. From value, A up to 1 multiply each digit and store 4. The final stored value is factorial of AExampleimport java.util.Scanner;    public class Factorial {       public static void main(String args[]){          int i, factorial=1, number;          System.out.println("Enter the number to which you need to find the factorial:");          Scanner sc = new Scanner(System.in);          number = sc.nextInt();          for(i = 1; i

Java program to print a Fibonacci series

Samual Sam
Updated on 13-Mar-2020 05:27:25

2K+ Views

Fibonacci Series generates subsequent number by adding two previous numbers. Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.Fn = Fn-1 + Fn-2Algorithm1. Take integer variable A, B, C 2. Set A = 1, B = 1 3. DISPLAY A, B 4. C = A + B 5. DISPLAY C 6. Set A = B, B = C 7. REPEAT from 4 - 6, for n timesExampleLive Demopublic class FibonacciSeries2{    public static void main(String args[]) {       int a, b, c, i, n;       n = 10;       a = b = 1;       System.out.print(a+" "+b);       for(i = 1; i

StringTokenizer class in Java

V Jyothi
Updated on 05-Mar-2020 12:18:39

413 Views

The StringTokenizer class of the java.util package allows an application to break a string into tokens.This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.Its methods do not distinguish among identifiers, numbers, and quoted strings.This class methods do not even recognize and skip comments.ExampleLive Demoimport java.util.*;   public class Sample {    public static void main(String[] args) {         // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");         // checking next token       System.out.println("Next token is : " + st.nextToken());   }     }OutputNext token is : Come

Match all occurrences of a regex in Java

Arnab Chakraborty
Updated on 23-Jun-2020 14:46:06

164 Views

public class RegexOccur {    public static void main(String args[]) {       String str = "java is fun so learn java";       String findStr = "java";       int lastIndex = 0;       int count = 0;       while(lastIndex != -1) {          lastIndex = str.indexOf(findStr,lastIndex);          if(lastIndex != -1) {             count ++;             lastIndex += findStr.length();          }       }       System.out.println(count);    } }Output2

Java regex to exclude a specific String constant

Arnab Chakraborty
Updated on 24-Jun-2020 07:26:33

980 Views

regex ^((?!kk).)*$ returns true if a line does not contain kk, otherwise returns falseExamplepublic class RegTest {    public static void main(String[] args) {       // TODO Auto-generated method stub       String s="tutorials";       boolean i=s.matches("^((?!kk).)*$");       System.out.println(i);    } }

How to test if a Java String contains a case insensitive regex pattern

Arnab Chakraborty
Updated on 24-Jun-2020 07:29:15

221 Views

the syntax? i:x makes the string search case-insensitive. for egpublic class RegCaseSense {    public static void main(String[] args) {       String stringSearch = "HI we are at java class.";       // this won't work because the pattern is in upper-case       System.out.println("Try this 1: " + stringSearch.matches(".*CLASS.*"));         // the magic (?i:X) syntax makes this search case-insensitive, so it returns true       System.out.println("Try this 2: " + stringSearch.matches("(?i:.*CLASS.*)"));    } }

Constructor overloading in Java

Sravani S
Updated on 05-Mar-2020 12:22:53

6K+ Views

Yes! Java supports constructor overloading. In constructor loading, we create multiple constructors with the same name but with different parameters types or with different no of parameters.ExampleLive Demopublic class Tester {      private String message;      public Tester(){       message = "Hello World!";    }    public Tester(String message){       this.message = message;    }      public String getMessage(){       return message ;    }      public void setMessage(String message){       this.message = message;    }      public static void main(String[] args) {       Tester tester = new Tester();       System.out.println(tester.getMessage());           Tester tester1 = new Tester("Welcome");       System.out.println(tester1.getMessage());      } }   OutputHello World! Welcome

Anonymous object in Java

Janani Jaganathan
Updated on 25-Aug-2022 10:00:49

12K+ Views

Anonymous object in Java means creating an object without any reference variable. Generally, when creating an object in Java, you need to assign a name to the object. But the anonymous object in Java allows you to create an object without any name assigned to that object. So, if you want to create only one object in a class, then the anonymous object would be a good approach. Reading this article, you will learn what an anonymous object is and how to create and use anonymous objects in Java. Let's get started! Anonymous Object in Java Anonymous means Nameless. An ... Read More

Advertisements