0% found this document useful (0 votes)
6 views24 pages

JAVA FIRST CLASS ARRAYS (1)

The document provides a comprehensive overview of arrays in Java, including their definition, syntax, and various examples of array manipulation. It covers different data types, initialization, and operations such as finding maximum values, counting occurrences, and exception handling. Additionally, it includes practical examples of using the Scanner class for user input and demonstrates basic sorting and searching techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

JAVA FIRST CLASS ARRAYS (1)

The document provides a comprehensive overview of arrays in Java, including their definition, syntax, and various examples of array manipulation. It covers different data types, initialization, and operations such as finding maximum values, counting occurrences, and exception handling. Additionally, it includes practical examples of using the Scanner class for user input and demonstrates basic sorting and searching techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ARRAYS

 Arrays is a collection of similar data types and occupies continuous


memory allocation, arrays length should be integer type.
 Arrays length should not represent negative, but it’s should
indicates zero.
 Arrays have a keyword (new)-new indicates object (Because object
is memory reference-new is keyword for objects).
 In object, it’s has states and behaviour. For arrays length is a
behaviour.

How to indicate Arrays in java


int array []= new int[5];
PROGRAM - 1
package org.program;
public class Program1 {
public static void main(String[] args)
{
Int [] arr = new int[5];
arr[0]= 12;
arr[1]= 32;
arr[2]= 42;
arr[3]= 72;
arr[4]= 82;
System.out.println(arr);
}
}
OUTPUT – { 12,32,42,72,82}`
PROGRAM – 2
package org.program;
public class Program1 {
public static void main(String[] args) {
byte[] arr = new byte[5];
int[] arr1 = new int[5];
short[] arr2 = new short[5];
long[] arr3 = new long[5];
float[] arr4 = new float[5];
double[] arr5 = new double[5];
char[] arr6 = new char[5];
boolean[] arr7 = new boolean[5];
String[] arr8 = new String[5];
System.out.println("my byte value is "+ arr);
System.out.println("int value is "+ arr1);
System.out.println("short "+arr2);
System.out.println("long "+arr3);
System.out.println("float "+arr4);
System.out.println("double "+arr5);
System.out.println("char "+arr6);
System.out.println("boolean "+arr7);
System.out.println("string "+arr8);
}
}
OUTPUT
my byte value is [B@7852e922
int value is [I@4e25154f
short [S@70dea4e
long [J@5c647e05
float [F@33909752
double [D@55f96302
char [C@3d4eac69
boolean [Z@42a57993
string [Ljava.lang.String;@75b84c92

(70dea4e, 5c647e05, 5c647e05, 33909752,55f96302,


3d4eac69, 42a57993, 75b84c92- is hash value)

PROGRAM-3

package org.program;
public class Program1 {
public static void main(String[] args) {
byte[] arr = new byte[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("byte "+arr[i]);
}
int[] arr1 = new int[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("int "+arr1[i]);
}
short[] arr2 = new short[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("short "+arr2[i]);
}
long[] arr3 = new long[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("long "+arr3[i]);
}
float[] arr4 = new float[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("float "+arr4[i]);
}
double[] arr5 = new double[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("double "+arr5[i]);
}
char[] arr6 = new char[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("char "+arr6[i]);
}
boolean[] arr7 = new boolean[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("boolean "+arr7[i]);
}
String[] arr8 = new String[2];
for (int i = 0; i < arr.length; i++) {
System.out.println("string "+arr8[i]);
}
}
}

OUTPUT:
byte 0
byte 0
int 0
int 0
short 0
short 0
long 0
long 0
float 0.0
float 0.0
double 0.0
double 0.0
char
char
Boolean False
Boolean False
string null
string null

PROGRAM-4
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,2,3,4,5};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
OUTPUT:
1
2
3
4
5

PROGRAM-5
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,2,3,4,5};
for (int i = 0; i < arr.length; i=i+2) {
System.out.println(arr[i]);
}
}
}
OUTPUT:
1
3
5

PROGRAM-6

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,2,3,4,5};
for (int i = 1; i < arr.length; i=i+2) {
System.out.println(arr[i]);
}
}
}
OUTPUT:
2
4

PROGRAM-7
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,1,2,3,4,5};
for (int i = 0; i < arr.length; i=i+1) {
if(arr[i]==1) {
System.out.println(arr[i]);
}
}
}
}
OUTPUT:
1
1

PROGRAM-8

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,1,2,3,4,5};
int count=0;
for (int i = 0; i < arr.length; i=i+1) {
if(arr[i]==1) {
count++;
}
}
System.out.println(count);
}
}

OUTPUT:
2

PROGRAM-9
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {1,1,2,3,4,5};
int max=0;
for (int i = 0; i < arr.length; i++) {
if(max<arr[i]) {
max=arr[i];
}
}
System.out.println(max);
}
}
OUTPUT:
5

PROGRAM-10
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {70,10,20,30,40,50};
int max=0;
int secMax=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]>max) {
secMax=max;
max=arr[i];
}
else if (arr[i]>secMax) {
secMax=arr[i];
}
}
System.out.println(max);
System.out.println(secMax);
}
}
OUTPUT:
70
50

PROGRAM-11
WRAPPER CLASS is used
Primitive class
byte
short
int
long
float
double
char
boolean

corresponding classes of java in primitive


class

byte -Byte
short -Short
int -Int
long -Long
float -Float
double -Double
char -Charecter
boolean –Boolean

In java constant is set as final in


corresponding class.
Constant is always in capital letter.
Always a static varible

PROGRAM-12

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {-70,-10,-20,-30,-40,-50};
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
}
}

OUTPUT:
-2147483648
2147483647

PROGRAM-13

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {-70,-10,-20,-30,-40,-50};
int max=Integer.MIN_VALUE;
int secMax=Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if(arr[i]>max) {
secMax=max;
max=arr[i];
}
else if (arr[i]>secMax) {
secMax=arr[i];
}
}
System.out.println(max);
System.out.println(secMax);
}
}
OUTPUT:
-10
-20

PROGRAM-13

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
String s = "12345";
System.out.println(Integer.parseInt(s));
}
}

OUTPUT:
12345

PROGRAM-14

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
String s = "Preethi12345";
System.out.println(Integer.parseInt(s));
}
}
OUTPUT:
Exception in thread "main"
java.lang.NumberFormatException: For input
string: "Preethi12345"at
java.lang.NumberFormatException.forInputString(
Unknown Source)at
java.lang.Integer.parseInt(Unknown Source)at
java.lang.Integer.parseInt(Unknown Source)at
org.program.Program1.main(Program1.java:6)

String should be in integer only otherwise


error is throws in java complier.
Exception Handling :

PROGRAM-15

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
String s = "Preethi12345";
try {
int i=Integer.parseInt(s);
System.out.println("yes");
}
catch (NumberFormatException nfe){
System.out.println("no");
}
}
}

OUTPUT:
No

PROGRAM-16
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
String s = "12345";
try {
int i=Integer.parseInt(s);
System.out.println("yes");
}
catch (NumberFormatException nfe){
System.out.println("no");
}
}
}

OUTPUT:
Yes

PROGRAM-16

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,20,40,50,60,70};
for (int i = 0; i < arr.length; i++) {
if(arr[i]==20)
{
System.out.println(arr[i]);
}
}
}
}

OUTPUT:
20
20
package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,20,40,50,60,70};
for (int i = 0; i < arr.length; i++) {
if(arr[i]==20)
{
System.out.println(i);
}
}
}
}

OUTPUT:
1
2

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,20,40,50,60,70};
int count =0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]==20)
{
count++;
}
}
System.out.println(count);
}
}
OUTPUT:
2

SELECTION SORTING

PROGRAM-17

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,30,40,50,60,70};
boolean keyispresent= false;
for (int i = 0; i < arr.length; i++) {
if(arr[i]==25)
{
keyispresent=true;
System.out.println("yes");
break;
}
}
if(keyispresent==false)
{
System.out.println("no");
}
}
}
OUTPUT:
no

package org.program;
import java.util.Arrays;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,30,40,50,60,70};
boolean keyispresent= false;
for (int i = 0; i < arr.length; i++) {
if(arr[i]==20)
{
keyispresent=true;
System.out.println("yes");
break;
}
}
if(keyispresent==false)
{
System.out.println("no");
}
}
}

OUTPUT:
Yes

PRIME NUMBER

package org.program;
public class Program1 {
public static void main(String[] args) {
int num =13;
boolean keyispresent= true;
for (int i = 2; i < num; i++) {
if(num%i==0)
{
keyispresent=false;
System.out.println("no");
break;
}
}
if(keyispresent==true)
{
System.out.println("yes");
}
}
}

OUTPUT:
Yes

SCANNER Class

package org.program;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number:");
int a = sc.nextInt();
System.out.println("number is "+ a);
}
}

OUTPUT:
Enter the number:
1
number is 1

ARRAY OF SCANNER CLASS

package org.program;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
int arr[]= new int[5];
for (int i = 0; i < arr.length; i++) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number:");
int a = sc.nextInt();
System.out.println("number is "+ a);
}
}
}

OUTPUT:
Enter the number:
1
number is 1
Enter the number:
2
number is 2
Enter the number:
3
number is 3
Enter the number:
4
number is 4
Enter the number:
5
number is 5

PROGRAM-18

package org.program;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
Scanner arrayLength= new Scanner(System.in);
System.out.println("Enter the length:");
int length = arrayLength.nextInt();
int arr[]= new int[length];
for (int i = 0; i < arr.length; i++) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number:");
int a = sc.nextInt();
System.out.println("number is "+ a);
}
}
}
OUTPUT:
Enter the length:
5
Enter the number:
1
number is 1
Enter the number:
2
number is 2
Enter the number:
3
number is 3
Enter the number:
4
number is 4
Enter the number:
5
number is 5

package org.program;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
int arr[]= {10,20,30,40,50};
for (int i = 0; i < arr.length; i++) {
int temp=arr[0];
for (i = 0; i < arr.length-1; i++) {
arr[i]=arr[i+1];
}
arr[i]=temp;
for (i = 0; i < arr.length; i++) {
System.out.println("after changing of array
"+arr[i]);
}
}
}
}

OUTPUT:
after changing of array 20
after changing of array 30
after changing of array 40
after changing of array 50
after changing of array 10

You might also like