Tutorials Point, posted Just Now

Java Examples Bubble Sort

public class HelloWorld {
   static void bubbleSort(int[] arr) {
      int n = arr.length;
      int temp = 0;
      for(int i = 0; i < n; i++) {
         for(int j=1; j < (n-i); j++) {
            if(arr[j-1] > arr[j]) { 
               temp = arr[j-1]; 
               arr[j-1] = arr[j];
               arr[j] = temp;
            } 
         } 
      } 
   }  
   public static void main(String[] args) {
      int arr[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 }; 
      System.out.println("Array Before Bubble Sort");
      
      for(int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      } 
      System.out.println();
      bubbleSort(arr);
      System.out.println("Array After Bubble Sort");
      
      for(int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      } 
   }  
}  

Tutorials Point, posted 10m 59s ago

Java Examples Linear Search

public class HelloWorld {
   public static void main(String[] args) {
      int[] a = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
      int target = 0;
      
      for (int i = 0; i < a.length; i++) {
         if (a[i] == target) {
            System.out.println("Element found at index " + i);
            break;
         } 
      } 
   }
}

Tutorials Point, posted 13m 1s ago

Java Examples Array Sort

import java.util.Arrays;

public class MainClass {
   public static void main(String args[]) throws Exception {
      int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
      Arrays.sort(array);
      printArray("Sorted array", array);
      int index = Arrays.binarySearch(array, 2);
      System.out.println("Found 2 @ " + index);
   }
   private static void printArray(String message, int array[]) {
      System.out.println(message + ": [length: " + array.length + "]");
      
      for (int i = 0; i < array.length; i++) {
         if(i != 0) {
            System.out.print(", ");
         }
         System.out.print(array[i]);                     
      }
      System.out.println();
   }
}

Tutorials Point, posted 24m 56s ago

Java Examples String buffering

public class HelloWorld {
   public static void main(String []args) {
      StringBuffer sb = new StringBuffer("hello");
      sb.append("world");
      sb.insert(0, " Tutorialspoint");
      System.out.print(sb);
   }
}

Tutorials Point, posted 27m 15s ago

Java Examples Buffers Strings by Using emit()

public class StringBuffer {
   public static void main(String[] args) {
      countTo_N_Improved();
   }
   private final static int MAX_LENGTH = 30;
   private static String buffer = "";
   
   private static void emit(String nextChunk) {
      if(buffer.length() + nextChunk.length() > MAX_LENGTH) {
         System.out.println(buffer);
         buffer = "";  
      }
      buffer += nextChunk;
   }
   private static final int N = 100;
   private static void countTo_N_Improved() {
      for (int count = 2; count<= N; count = count+2) {
         emit(" " + count);
      }
   }
}

Tutorials Point, posted 29m 50s ago

Java Examples String Unicode

public class StringUniCode {
   public static void main(String[] args) {
      String test_string = "Welcome to TutorialsPoint";
      System.out.println("String under test is = "+test_string);
      
      System.out.println("Unicode code point at" 
         +" position 5 in the string is = "
         +  test_string.codePointBefore(5));
   }
}

Tutorials Point, posted 32m 20s ago

Java Examples Concatenation String2

public class HelloWorld { 
   public static void main(String []args) {
      String s = "Hello"; 
      s = s.concat("word");
      System.out.print(s);
   }
}

Tutorials Point, posted 36m 26s ago

Java Examples Concatenate Strings

public class StringConcatenate {
   public static void main(String[] args) {
      long startTime = System.currentTimeMillis();
      
      for(int i = 0;i<5000;i++) {
         String result = "This is"
            + "testing the"
            + "difference"+ "between"
            + "String"+ "and"+ "StringBuffer";
      }
      long endTime = System.currentTimeMillis();
      System.out.println("Time taken for string" 
         + "concatenation using + operator : " 
         + (endTime - startTime)+ " ms");
      long startTime1 = System.currentTimeMillis();
      
      for(int i = 0;i<5000;i++) {
         StringBuffer result = new StringBuffer();
         result.append("This is");
         result.append("testing the");
         result.append("difference");
         result.append("between");
         result.append("String");
         result.append("and");
         result.append("StringBuffer");
      }
      long endTime1 = System.currentTimeMillis();
      System.out.println("Time taken for String concatenation" 
         + "using StringBuffer : "
         + (endTime1 - startTime1)+ " ms");
   }
}

Tutorials Point, posted 46m 13s ago

Java Examples String formatting-2

public class HelloWorld {
   public static void main(String []args) {
      String name = "Hello World";
      String s1 = String.format("name %s", name);
      String s2 = String.format("value %f",32.33434);
      String s3 = String.format("value %32.12f",32.33434);
      System.out.print(s1);
      System.out.print("\n");
      System.out.print(s2);
      System.out.print("\n");
      System.out.print(s3);
      System.out.print("\n");
   }
}

Tutorials Point, posted 49m 15s ago

Java Examples String Formatting

import java.util.*;

public class StringFormat{
   public static void main(String[] args){
      double e = Math.E;
      System.out.format("%f%n", e);
      System.out.format(Locale.GERMANY, "%-10.4f%n%n", e);
   }
}

1 2 3 4 5 6 7 ... 279 Next
Advertisements
Loading...