Posts

Showing posts from September, 2020

Features of ArrayList over Array in java

  Array ArrayList Add an element at the end of the array This action is not supported list . add ( s ) ; Add an element in the middle of the array This action is not supported list . add ( 15 , s ) ; Add an element at the beginning of the array This action is not supported list . add ( 0 , s ) ; Delete an element from the array We could delete an element with   list [3] = null . But this would leave a 'hole' in the array. list . remove ( 3 ) ;

Difference between Array and ArrayList in Java

  Program using Array : public static void main ( String [ ] args ) { Reader r = new InputStreamReader ( System . in ) ; BufferedReader reader = new BufferedReader ( r ) ; // Read strings from the keyboard String [ ] list = new String [ 10 ] ; for ( int i = 0 ; i < list . length ; i ++ ) { String s = reader . readLine ( ) ; list [ i ] = s ; } // Display the contents of the array for ( int i = 0 ; i < list . length ; i ++ ) { int j = list . length - i - 1 ; System . out . println ( list [ j ] ) ; } } Program using ArrayList : public static void main ( String [ ] args ) { Reader r = new InputStreamReader ( System . in ) ; BufferedReader reader = new BufferedReader ( r ) ; // Read strings from the keyboard ArrayList < String > list = new ArrayList < String > ( ) ; for ( int i = 0 ; i < 10 ; i ++ ) { String s = reader . readLine ( ) ; list . add ( s ) ; } // Di...

Java Program to create a large array and copy its values equally to two small arrays and display the second array only

  PROGRAM: public class Solution {                   public static void main ( String [ ] args ) throws Exception {    BufferedReader br= new  BufferedReader ( new InputStreamReader ( System . in ) ) ;              int [ ] l= new int [ 20 ] ;                                                                                                                                                                               ...

Java program to display the elements of an array in STRAIGHT and in REVERSE order

PROGRAM: public class Main { public static void main(String[] args) { int[] num ={1,2,3,4,5};     System.out.println("REVERSE ORDER:"): for(int i=num.length-1;i>0;i--) { System.out.println(num[i]); }         System.out.println("STRAIGHT ORDER:"): for(int i=0;i<num.length;i++) { System.out.println(num[i]);}      } } OUTPUT: REVERSE ORDER: 5 4 3 2 1 STRAIGHT ORDER: 1 2 3 4 5

Java progarm to Display the elements in an array individually

  public class Main { public static void main(String[] args) { int[] num ={1,5,6,77,88}; System.out.println(num[0]); System.out.println(num[1]); System.out.println(num[2]); System.out.println(num[3]); System.out.println(num[4]); } } OUTPUT: 1 5 6 77 88

Java program to display the elements in an array

PROGRAM 1: package com.company; import java.io.*; public class Main { public static void main(String[] args) { int[] num ={1,5,6,77,88}; for(int i=0;i<num.length;i++) {System.out.println(num[i]); }}} PROGRAM 2 : package com.company; import java.io.*; public class Main { public static void main(String[] args) { int[] num ={1,5,6,77,88}; for(int i:num) { System.out.println(i); }}} OUTPUT : Output for both Progarm 1 and Program 2 are same as below 1 2 6 77 88

Java Program to display the length of the elements in an array containing string values to another array.

PROGRAM: public   class Solution {                                                                                                             public static void main ( String [ ] args ) throws Exception {                                              BufferedReader br= new BufferedReader ( new InputStreamReader ( System . in ) ) ;        String [ ] s= new String [ 10 ] ;                                                           ...