Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
.
Chapter 3
Array in java
www.raparinweb.fulba.com
M hiwa ahmad aziz
1
Array
Definition
• An array is a group/collection of variables of the same type that are referred
to by a common name.
• A specific element in an array is accessed by its index (subscript).
save 3 score in variable
save 3 score in array
java programing : Lecturer ( m hiwa ahmad aziz)
2
Declaration array
• we can create an array of integers, characters, String and objects, etc.
• Size of an array can’t be changed after the array is created.
• Length of an array can be obtained as:
array_name.length For example, score.length is 7
java programing : Lecturer ( m hiwa ahmad aziz)
3
Initializing Arrays
1. Using the assignment operator (=) to initialize an array
int [ ] score= new int [ 3 ];
score[0] = 78;
score[1] = 88;
score[2] = 53;
2. Initialize at time of declaration - filling by list.
double [ ] température = { 13.5, 18.4, 19.6, 21.4};
3. Using a for loop and user input to initialize an array:
int [ ] score= new int [ 8 ];
for(int i= 0; i< 8; i++)
{
score[i] = scan.nextInt();
}
java programing : Lecturer ( m hiwa ahmad aziz)
4
Example 3.1
Write program that take four Score lessons with the name of lessons from the
user and print as(in) table.
With out Scanner
public class printArrayt {
public static void main(String[] args) {
String subject []= {"data","oop","math","java"};
double score[] = { 78,80,53,90};
System.out.println("subject"+"\t\t"+"score");
System.out.println("---------------------");
for(int i=0;i<score.length;i++)
// for print array use loop(for)
System.out.println(subject[i]+"\t\t"+score[i]);
}
}
java programing : Lecturer ( m hiwa ahmad aziz)
5
Example 3.2 (linear search )
Write program that enter 7 element in array with user and enter a number for search in array.
import java.util.Scanner;
class LinearSearch {
public static void main(String args[])
{
Scanner scan= new Scanner(System.in);
int i, search;
int[] array = new int[7];
// instantiate the array
System.out.println("Enter " + 7 + " integers");
for (i = 0; i < 7 ; i++)
// enter 10 number in array
array[i] = scan.nextInt();
System.out.println("Enter value to find");
search = scan.nextInt();
for (i = 0; i < 7 ; i++) {
if (array[i] == search) {
System.out.println(search + " is present at location " + (i + 1) + ".");
break; }
} if (i == 7)
// search in array
System.out.println(search + " is not present in array.");
}
}
java programing : Lecturer ( m hiwa ahmad aziz)
6
Example 3.3
Write a program enter 10 number in array and send to method for find average array.
import java.util.Scanner;
public class FindSumArray {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int [ ] number = new int [ 10];
// instantiate the array
int I ;
System.out.println(“enter 10 number for find average");
for ( i = 0; i < 10; i++ )
// enter 10 number in array
number[ i ] = scan.nextInt();
double avg = find_avg(number);
// invoke the method
System.out.println("The average is" +avg + ".");
}
public static int find_avg ( int number [ ])
//method definition to find sum
{
int i, sum = 0;
for(i=0; i<10; i++)
{
sum = sum + number[ i ];
}
double avg= sum/10;
return (avg);
}
}
java programing : Lecturer ( m hiwa ahmad aziz)
7
Example 3.4.1
Write a program that take a number at the user and find the factorial.
import java.util.Scanner;
class factorial {
public static void main (String[] args) {
Scanner scan=new Scanner(System.in);
int x= scan.nextInt();
System.out.println("enter a number for find factorial");
int f=1;
for (int i=1;i<=x;i++)
f=f*i;
System.out.println(“final result is = ” +f);
} }
// tries this program for number 5
java programing : Lecturer ( m hiwa ahmad aziz)
8
Example 3.4.2
Write a program that take a number at the user and find the factorial with
method.
import java.util.Scanner;
public class Facorial {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("enter a number for find factorial");
int x= scan.nextInt();
factmethod( x);
// invoke the method
}
public static void factmethod ( int x){
int f=1;
for (int i=1;i<=x;i++)
f=f*i;
System.out.println("the factorial "+x +" is = " +f);
}
}
java programing : Lecturer ( m hiwa ahmad aziz)
9
Example 3.4.3
Write a program that take array of 10 number at the user and find the factorial with method.
import java.util.Scanner;
public class Facorial {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
double x[] = new double[5];
// define array x
System.out.println("enter 5 number for find factorial");
for(int i=0;i<5;i++)
// enter 5 number with user in array x
x[i]= scan.nextDouble();
for(int i=0;i<5;i++){
System.out.print("\t "+x[i]);}
// print array x
System.out.print("\n ");
for(int i=0;i<5;i++){
x[i]=factmethod(x[i]);
// every index x send to factmethod and print them
System.out.print("\t "+x[i]); }
}
public static double factmethod(double x){
int f=1;
for (int i=1;i<=x;i++)
f=f*i;
return f;
}
}
java programing : Lecturer ( m hiwa ahmad aziz)
10
Example 3.5
Write program that take four Score lessons with the name of lessons from the
user and print as(in) table.
With Scanner
import java.util.Scanner;
public class Arraytemplet {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int score[] = new int[4];
String subject[] = new String[4];
System.out.println("enter 4 subject lessones");
for(int i=0;i<subject.length;i++)
subject[i] = scan.nextLine();
System.out.println("enter 4 score lessones");
for(int i=0;i<score.length;i++)
score[i] = scan.nextInt();
System.out.println("subject"+"\t\t"+"score");
System.out.println("---------------------");
for(int i=0;i<score.length;i++)
System.out.println(subject[i]+"\t\t"+score[i]);
}
}
// define array score and subject
// enter 4 subject with user in array
// enter 4 score with user
// print head table
// print subject and score
java programing : Lecturer ( m hiwa ahmad aziz)
11
Exam
Write program that take four name student and Score lessons with
the name of lessons from the user and print as(in) table.
java programing : Lecturer ( m hiwa ahmad aziz)
12