Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
java set1 program.pdf
1. 12 3 4 5
6
java.1ang.ArrayIndexOutotBoundsException: Index 6 out of bounds for length 5
Give correct index
BUILD SUCCESSFUL (total time: 7 seconds)
.va FlbonacciRecursion.javaX MergeSortWithOutExtraSpace.java X
Bstjava Xy Treejava x
ExceptionHandling java XSumOffwoNumbers.java XArayinde
History P
pusiec eLass APPEYindexxceptIon (
Source
I
public static void main (String tl args)
Scanner sc=new Scanner (System. in) ;
int size=sc.nextInt ()
int arr []=new int [size]
10
for (int i=0;i<size;i++)
Ll
arr[i]=sc.nextInt()
tryl
int index=sc
.nextInt () ;
system. out.println (arr (index] ) ;
18
19
catch (ArrayIndexoutofBoundsException e)
20
System.out.println (e) ;
System. 0ut.println ("Give correct index")
21
22
23
2. I
o
java. lang.Arit.hmeticException: by zero
Give correct divisor
BUILD SUCCESSFUL (total time: 4 seconds)
va
FlbonacciRecursion.java X
MergeSortWithOutExtraSpace.java X
Bstjava X
Treejava ExceptionHarndling.java XSumOfToNumbersjava
Source History e
package sumoftwonumbers;
import java.util.*
public class
Sumo fTwoNumber
public static void main (String [] args) t
Scanner sc=new Scanner (system. in);
int x=sc.nextInt ()://getting and input
int y=sc.nextInt ()i//getting an input
try{//try block
int div=x/y;
System. Out.printin ("Division of two numbers is: "+div) :
catch(ArithmeticException e){//ctach bloack
System. Out .println (e)
system. out .println ("Give correct divisor") ;
3. erence between Lis, set and Map in
Java
Listinterface in_Java is a sub-interface of
the Java collections interface. It contains
the index-based methods to insert, update,
delete, and search the elements. It can
have duplicate elements also. We can also
store the null elements in the list. List
preserves the insertion order, it allows
positional access and insertion of
elements. It found in the java.util package.
Let's consider an example for a better
understanding where you will see how
you can add elements by using a list
interface in java.
4. The Set follows the unordered way and it
found in java.util package and extends the
collection interface in java. Duplicate item
will be ignored in Set and it will not print
in the final output. Let's consider an
example for a better understanding where
you will see how you can add elements by
using a set interface in java. Let's have a
look.
5. The Java Map interface, java.util.Map
represents a mapping between a key and a
value. More specifically, a Java Map can
store pairs of keys and values. Each key is
linked to a specific value. Once stored in a
Map, you can later look up the value using
just the key. Let's consider an example for
better understanding where you will see
how you can add elements by using the
Map interface in java. Let's have a look.
6. jst set
N does not The map do
face allows
allow duplicate allow du
duplicate elements. elements
elements
do
Set
list
maintain
not
The map als
any
not
The
maintains maintai
insertion
insertion order. insertion orde
order.
The map all
We can add any But in set single null
number of null almost only one most and
values. null value. number of
values.
List
Set
implementation Map impleme
implementation
are
classes
List,
HashSet,
classes classes are Ha
are
Array HashTable, T
ConcurrentHa
LinkedHasnandLinkedHa
LinkedList.
and TreeSet.
7. Run
Output
1 // Online Java Compiler
java cp tmp/jLlghgvP08 Helloworld
30
2 / Use this editor to write, compile and run your Java code online
3 import java.util.*;
4 class HelloWorld {
public statiC void main(String[] args) {
Scanner sc new Scanner(System.in)
List<Integer list new ArrayList<>):
list.add(10):
list.add(20)
list.add(30);
System.out.print(list.get(2)); //get value of list at index 2
8
9
10
11
12
13
8. Tun
Printing before adding (10, 12, 301
printing after adding duplicate (10, 12, 301
BUILD SUcCESSEUT (total time: 0
seconds)
MergeSortWithOutExtraSpace.java Bstjava X
Tree.java ExceptionHandling.java X
SumOffwoNurnbersjava ArrayindeExcoption.jnva
History
arce
package exampleset:
import java.util.*
public class Exampleset {
public static void main(String[] args)
Scanner sC=new Scanner (system.in) ;
Set<Integer> set=new HashSet<>()
set.add (10) ;
set.add(12)
set.add(30)
System. out -println ("Printing before adding "+set);
set.add(10) 1//adding duplica te
System,out.println("printing after adding duplicate "+set)
9. run:
1 2 3 45
Elements in the map-> (1-0, 2-1, 3-2, 43, 5-4)
BUILD SUcCESSPUL (total t ime: 3
second)
.va BstjavaX Treejava Exception Handlingjava X
SumOflwoNumbersjava ArraylndexException.java Erampleset.java ExampleHashMapjava
Source History P 2
package examplehashmap
import
java.util.*
public class ExampleHashMap
public static void
main(Stringt] args)
Scanner sc=new
scanner(system.in)
Map Integer,Integer> map-new HashMap<> () 1//creatig an hashMap
int n-sc.nextInt ()
for (int i-01i<nii++)
int numsc.nextInt ()
map.put (num, i)1//putting intp mapP
System. out .println ("Elements in the map-> "+map)
system. out.printin(map.get(2))#//get ting an index
18
19
20
10. Eun
[Red, Green, Orange, White, Black]
BUILD SUCCESSFUL (total time:0 seconds)
va Tree.java X ExceptionHandling java SumOfTwoNumbers.java ArraylndexException.java ExampleSet.java X ExarmplettashMap jnva ExampleArray
Source History P 9
package examplearraylist;
import java. util. *
public class ExampleArrayList (
public static void
main (String[] args)
Scanner sc=new Scanner (System. in)
List<string> 1ist-new ArrayList<>()
list.add ("Red"):
list. add ("Green");
list.add ("Orange") ;
1ist.add ("White")1
list.add ("Black")
system.out-printin (list);
17
18
19
20
11. run:
Beforeremoving the third element in the 1ist [Red, Green, Orange, White, Black]
After removing the third element in the list [Red, Green, orange, Black]
BUILD SUCCESSFUL (total time: 0 seconds)
I
.va SumOffwoNumbers.java X
ArraylndexException.java ExampleSet.java X
ExampleHashMapjava ExampleArraylistjava X ExampleArraylist2.java
Source History
public static void main (Stringl] args) (
Scanner sc=new Scanner(system.in)
List<string> list=new ArrayList<> () ://creating an 1ist
10
11 list.add ("Red") ;
list.add ("Green") ;//adding
list.add ("Orange") ;
12
13
list.add("White") ;
list.add("Black")
System. Out.println ("Be fore removing the third element in the 1ist "+list) ;
list.remove(3) ;//removing|
system. out .println ("After removing the third element in the list "+list) ;
14
16
19
20