Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Programming in Java - 11

The document discusses arrays in Java, including: 1) Defining and initializing arrays, including reserving space and assigning initial values. 2) Accessing array elements using indexes from 0 to length-1. 3) Examples of initializing arrays with set values, random values, and user input, and printing arrays. 4) Finding the maximum and minimum values in an array. 5) Sorting an array in ascending order.

Uploaded by

abwyasra240
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Programming in Java - 11

The document discusses arrays in Java, including: 1) Defining and initializing arrays, including reserving space and assigning initial values. 2) Accessing array elements using indexes from 0 to length-1. 3) Examples of initializing arrays with set values, random values, and user input, and printing arrays. 4) Finding the maximum and minimum values in an array. 5) Sorting an array in ascending order.

Uploaded by

abwyasra240
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Programming in JAVA

11th session

Written by Dr. Mohamad Obaid


Outline

• Array definition

• Array initialization

• Enter and printing an Array

• Maximum and minimum value of array

• Array sorting
Array
• What’s array?
- array is many places where data is stored for temporary time.
- When we define and create a array, there are specific numbers of
adjacent places in the memory are used to save data in its.
- Access to the saved data done by use array name and Index,
where the index starts from zero.

• What’s the benefit of array?


- The benefit of using array is the ability of saving large number of
values under one name (the array name) without necessary to save
every value in a separate variable.
Array Definition
• We can define one dimension array of integer values by the
following statement:
int a[];
• We can define 10 places for this array by the following
statement:
a = new int[10];
• We can merge the two previous statement in one statement:
int a[] = new int[10];
Array Definition
• In Java language, the index of element’s position is written between [] after array
name. This index is a number located between 0 and n-1. where n is the count of
array elements.

• In the begin of program all integer array elements has zero value.

• If we need to start array elements with next values:

15 , 30 , 50 , 33 , 40 , 17 , 150 , 88 , 44 , 50

So we need to use these commands:


a[0]=15; a[1]=30; a[2]=50; a[3]=33; a[4]=40; a[5]=17; a[6]=150; a[7]=88; a[8]=44; a[9]=50;
Array Definition
• We can combine definition statement with reservation statement:
int a[]={15 , 30 , 50 , 33 , 40 , 17 , 150 , 88 , 44 , 50 };
So the array will be like this:

• To print the previous array:


for (int i=0 ; i < 10 ; i++)
System.out.println(a[i]);
Example 11.1: Program define an array has 10
elements and initialize it by the multiplicands of 5
then print this array
public static void main(String[] args) • We can merge the two loops in one:
{ public static void main(String[] args)
{
int i ;
int i ;
int a[]=new int[10];
int a[]=new int[10];
for (i=0;i<10;i++) for (i=0;i<10;i++)
a[i]=5*i+5; {
a[i]=5*i+5;
for (i=0;i<10;i++) System.out.print(a[i]+" ");
System.out.print(a[i]+" "); }
} }
Example 11.2: Program initialize an array consists of n
elements by Fibonacci series then print the contents of
array with its indexes using Dialog box
public static void main(String[] args)
{ int i,n ;
String result="";
n=Integer.parseInt(JOptionPane.showInputDialog("n="));
int a[]=new int[n];
a[0]=0; a[1]=1;
result+="index value\n";
for (i=2;i<n ; i++)
a[i]=a[i-1]+a[i-2];
for (i=0; i<n ; i++)
result+=i+" "+a[i]+"\n";
JOptionPane.showMessageDialog(null,result);
}
Example 11.3: Program initialize an array by n
random values and print it
public static void main(String[] args)
{ int i,n ;
n=Integer.parseInt(JOptionPane.showInputDialog("n="));
double a[]=new double[n];
for (i=0;i<n;i++)
{
a[i]=Math.random();
System.out.println(a[i]+" ");
}
}
Notes:
• The index must be positive and integer number, and not exceed the
count of elements in the array.
• The property a.length give the count of elements in array.
• We can put [] before array name:
Double [] a = new double [10];
• If the array hasn’t initial values so it has values according to its kind:
Type Initial value
int , byte , short , long 0
double , float 0.0
boolean false
String null
char \u0000 ‫فراغ‬
Example 11.4: Program Enter an array consists of n elements then print this
array as it entered, reversely ,maximum, minimum element, and print array
after ascending sorting
public static void main(String[] args)
{ int i,n,j,t,max,min ; String result="";
n=Integer.parseInt(JOptionPane.showInputDialog("n="));
int a[]=new int[n];
for (i=0; i<n ; i++)
a[i]=Integer.parseInt(JOptionPane.showInputDialog("num "+(i+1)));
result+="normal\n";
for (i=0; i<n ;i++)
result+=a[i]+" ";
result+="\nrevese\n";
for (i=n-1;i>=0;i--)
result+=a[i]+" ";
Example 11.4
max=a[0]; for (i=0;i<n-1;i++)
for (i=1;i<n;i++) for (j=i+1;j<n;j++)
if (a[i]>max) max=a[i]; if (a[i]>a[j])
min=a[0]; { t=a[i]; a[i]=a[j]; a[j]=t; }
result+="after sorting\n";
for (i=1;i<n;i++)
for (i=0;i<n ;i++)
if (a[i]<min) min=a[i];
result+=a[i]+" ";
result+="\nmax="+max+" min="+min+"\n";
JOptionPane.showMessageDialog(null,result);
}
}
Example 11.5:Program Enter the names and marks
for n students then print the successful student
names
public static void main(String[] args)
{
int i, n ;
String res="";
n=Integer.parseInt(JOptionPane.showInputDialog("n="));
String name[]=new String[n];
int mark[]=new int[n];
for(i=0;i<n;i++)
{
name[i]=JOptionPane.showInputDialog("name["+i+"]=");
mark[i]=Integer.parseInt(JOptionPane.showInputDialog("mark["+i+"]="));
if (mark[i]>=60)
res+=name[i]+"\n";
}
JOptionPane.showMessageDialog(null, res);
}

You might also like