Uploaded by Sabari Murugan Sivakumar

Arrays

advertisement
Page 1 of 11
Chapter 3: Arrays
Program 1: Printing an array by declaring its elements.
public class Arrays
{
public static void main(String[] args)
{
int x [] = {1, 2, 3, 4, 5};
System.out.println("x[0]="+x[0]);
System.out.println("x[1]="+x[1]);
System.out.println("x[2]="+x[2]);
System.out.println("x[3]="+x[3]);
System.out.println("x[4]="+x[4]);
}
}
Program 2: Printing an array using loop.
public class Array2
{
public static void main(String[] args)
{
int x [] = {1, 2, 3, 4, 5};
for(int i = 0; i<5; i++)
System.out.println("x[i]="+x[i]);
}
}
Program 3: Array for integer datatype.
public class ArrayInteger
{
public static void main(String[] args)
{
int x [] = {1, 2, 3, 4, 5};
System.out.println(x[0]);
System.out.println(x[1]);
System.out.println(x[2]);
System.out.println(x[3]);
System.out.println(x[4]);
}
}
Program 4: Finding the sum of the elements in the array.
public class ArraySum
{
public static void main(String[] args)
{
int x [] = {1, 2, 3, 4, 5};
int sum = 0;
for(int i = 0; i<5; i++)
sum = sum + x[i];
}
}
Page 2 of 11
Program 5: Finding the product of the elements in the array.
public class ArrayProduct
{
public static void main(String[] args)
{
int x [] = {1, 2, 3, 4, 5};
int product = 1;
for(int i = 0; i<5; i++)
product = product * x[i];
}
}
Program 6: Array for character datatype.
public class ArrayChar
{
public static void main(String[] args)
{
char x [] = {'d', 'h', 'a', 'v'};
System.out.println(x[0]);
System.out.println(x[1]);
System.out.println(x[2]);
System.out.println(x[3]);
}
}
Program 7: Array for string datatype:
public class ArrayString
{
public static void main(String[] args)
{
String x [] = {"Dhavedha", "Darshini", "is", "my", "name."};
System.out.println(x[0]);
System.out.println(x[1]);
System.out.println(x[2]);
System.out.println(x[3]);
System.out.println(x[4]);
}
}
Program 8: Defining the values of the elements of the array using Scanner class.
import java.util.Scanner;
public class ArrayScanner
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter 5 values:");
int x[] = new int[50];
for(int i = 0; i<=5; i++)
x[i] = scanner.nextInt();
System.out.println("The values entered are:");
for(int i = 0; i<=5; i++)
System.out.println(x[i]);
}
}
Page 3 of 11
Program 9: Defining the value of the elements of the array using the Input Stream Reader.
import java.io.*;
public class ArrayBuffered
{
public static void main(String[] args) throws IOException
{
InputStreamReader x = new InputStreamReader(System.in);
BufferedReader y = new BufferedReader(x);
System.out.println("Enter 5 values:");
int n[] = new int[5];
for(int i = 0; i<=5; i++)
{
n[i] = Integer.parseInt(y.readLine());
System.out.println(n[i]);
}
}
}
Program 10: Changing the value of element(s) in the array.
import java.util.Scanner;
public class ArrayChange
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter 5 values:");
int x[] = new int[50];
for(int i = 0; i<=5; i++)
x[i] = scanner.nextInt();
x[2] = 555;
x[4] = 225;
System.out.println("The values entered are:");
for(int i = 0; i<=5; i++)
System.out.println(x[i]);
}
}
Program 11: Shifting the values of the elements in the array.
import java.util.Scanner;
public class ArrayShift
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("\nHow many elements do you need? ");
int n = scanner.nextInt();
System.out.println("Enter the values of the elements of the array:");
int x[] = new int[50];
for(int i = 0; i<n; i++)
x[i] = scanner.nextInt();
System.out.println("Enter the position of the element to be shifted:");
int p = scanner.nextInt();
for(int i = n; i>=p; i--)
x[i] = x[i-1];
n++;
System.out.println("The shifted values of the array:");
for(int i = 0; i<n; i++)
Page 4 of 11
}
}
System.out.println(x[i]);
Program 12: Inserting a value of a new element in the given array.
import java.util.Scanner;
public class ArrayInsertion
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("\nHow many elements do you need? ");
int n = scanner.nextInt();
System.out.println("Enter the values of the elements of the array:");
int x[] = new int[50];
for(int i = 1; i<=n; i++)
x[i] = scanner.nextInt();
System.out.println("Enter the value of the element to be inserted:");
int v = scanner.nextInt();
System.out.println("Enter the position of the element to be changed:");
int p = scanner.nextInt();
for(int i = n; i>=p; i--)
x[i] = x[i-1];
x[p] = v;
n++;
System.out.println("The value inserted array:");
for(int i = 1; i<n; i++)
System.out.println(x[i]);
}
}
Program 13: Deleting an element.
import java.util.Scanner;
public class ArrayDeletion
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("\nHow many elements do you need? ");
int n = scanner.nextInt();
System.out.println("Enter the values of the elements of the array:");
int x[] = new int[50];
for(int i = 0; i<n; i++)
x[i] = scanner.nextInt();
System.out.println("Enter the position to be deleted:");
int p = scanner.nextInt();
for(int i = p+1; i<n; i++)
x[i-1] = x[i];
n--;
System.out.println("The value deleted array:");
for(int i = 0; i<n; i++)
System.out.println(x[i]);
}
}
Page 5 of 11
Program 14: Merging two arrays to form a single array.
import java.util.Scanner;
public class ArrayConcatenation
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("\nHow many elements do you need in the first array? ");
int n1 = scanner.nextInt();
System.out.println("How many elements do you need in the second array? ");
int n2 = scanner.nextInt();
int n3 = n1 + n2;
int x1[] = new int[50];
int x2[] = new int[50];
int x3[] = new int[50];
System.out.println("What are the values of the elements in the first array?");
for(int i = 0; i<n1; i++)
x1[i] = scanner.nextInt();
System.out.println("What are the values of the elements in the second array?");
for(int i = 0; i<n2; i++)
x2[i] = scanner.nextInt();
for(int i = 0; i<n1; i++)
x3[i] = x1[i];
for(int i = 0; i<n2; i++)
x3[n1+i] = x2[i];
System.out.println("The concatenated array:");
for(int i = 0; i<n3; i++)
System.out.println(x3[i]);
}
}
Program 15: Arranging the values of the elements in ascending or descending order using
selection sort.
import java.util.Scanner;
public class ArraySeletionSort
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("\nHow many elements do you need? ");
int n = scanner.nextInt();
System.out.println("Enter the values of the elements of the array:");
int x[] = new int[50];
for(int i = 0; i<n; i++)
x[i] = scanner.nextInt();
int temp = 0;
System.out.println("The selection sorted array:");
for(int i = 0; i<n; i++)
{
for(int j = i+1; j<n; j++)
{
if(x[i] > x[j])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
Page 6 of 11
}
}
}
for(int i = 0; i<n; i++)
System.out.println(x[i]);
Program 16: Finding whether an array has the value of an element using linear search.
import java.util.Scanner;
public class ArrayLinearSearch
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("\nHow many elements do you need? ");
int n = scanner.nextInt();
System.out.println("Enter the values of the elements of the array:");
int x[] = new int[50];
for(int i = 0; i<n; i++)
x[i] = scanner.nextInt();
System.out.println("Enter the value to be found:");
int v = scanner.nextInt();
boolean value = true;
for(int i = 0; i<n; i++)
if (x[i] == v)
value = true;
else
value = false;
if (value == true)
System.out.println(v+" has been found in the array.");
else
System.out.println(v+" has not been found in the array.");
}
}
Program 17: Finding the an array has the value of the element using binary search.
import java.util.Scanner;
public class ArrayBinarySearch
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int x[] = new int[50];
System.out.println("\nHow many elements do you need in the array? ");
int n = scanner.nextInt();
int f = 0;
int l = n;
int m;
m = (f+l)/2;
System.out.println("What are the values of the elements of the array?");
for(int i = 0; i<n; i++)
x[i] = scanner.nextInt();
int temp = 0;
System.out.println("Enter the value to be found:");
int v = scanner.nextInt();
for(int i = 0; i<n; i++)
{
for(int j = i+1; j<n; j++)
{
Page 7 of 11
if(x[i] > x[j])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
}
}
for(int i = 0; i<n; i++)
System.out.println(x[i]);
while(f <= l)
{
m = (f+l)/2;
if ( x[m] == v )
{
System.out.println(m+" has been found in the array.");
break;
}
else if ( x[m] < v )
{
f = m + 1;
}
else if ( x[m] > v )
{
l = m - 1;
}
}
if ( f > l )
{
System.out.println(v+" has not been found in the array.");
}
Page 8 of 11
Unit 3 – Arrays (Section A questions)
Write short answers
Question 1
What is meant by Dimensional Array?
A dimensional array is a structure created in the memory to represent a number of values of the
same data type with the variables having the same variable name along with different subscripts.
Question 2
Name the two types of Dimensional Array.
1. Single Dimensional Array
2. Double Dimensional Array
Question 3
What is the need of Dimensional Array? Explain.
Variables are useful for keeping track of a single piece of information but as we collect more and
more information, keeping the variables organised can be complicated. In such situations, we
need arrays to solve the problems in a much better and efficient way.
Question 4
'Array is a composite data type'. Explain this statement.
The data type that represents a number of similar or different data under single declaration is
called as composite data type. An array is a group or a collection of same type of variables.
Hence, Array is a composite data type.
Question 5
Define the following with their constructs:
(a) Single Dimensional Array
A Single Dimensional Array contains one row and one or more columns. The syntax of declaring a
Single Dimensional Array is:
<type> <array-variable>[] = new <type>[<size>];
OR
<type> [] <array-variable> = new <type>[<size>];
(b) Double Dimensional Array
Double Dimensional Array contains multiple rows and multiple columns. The syntax of declaring a
Double Dimensional Array
is:
<type> <array-variable>[][] = new <type>[<rows>][<columns>];
OR
<type> [][] <array-variable> = new <type>[<rows>][<columns>];
Differentiate between the following
Question 1
Subscript and Subscripted variable
Subscript is the index of the element in the array whereas Subscripted variable is the name of the
array when it is used with a subscript to access a single element of the array.
Example: m[1] here, m is the subscripted variable 1 is the subscript.
Question 2
char a[5] and int a[5]
char a[5] is an array of char data type that can hold 5 characters whereas int a[5] is an array of int
data type that can hold 5 integer values.
Question 3
Ordinary variable and Array variable
An ordinary variable can hold only one value whereas an array variable can refer to a group of
values of the same data type by using a subscript.
Page 9 of 11
Question 4
Sorting and Searching
Sorting :
• Sorting means to arrange the elements of the array in ascending or descending order.
• Bubble sort and Selection sort are examples of sorting techniques.
Searching :
• Searching means to search for a term or value in an array.
• Linear search and Binary search are examples of search techniques.
Question 5
Linear search and Binary search
Linear Search :
• Linear search works on sorted and unsorted arrays.
• Each element of the array is checked against the target value until the element is found or end
of the array is reached.
• Linear Search is slower.
Binary Search :
• Binary search works on only sorted arrays (ascending or descending).
• Array is successively divided into 2 halves and the target element is searched either in the first
half or in the second half.
• Binary Search is faster.
Question 6
Selection sort and Bubble sort
Selection sort :
• Selection sort selects the smallest element from unsorted sub-array and swaps it with the
leftmost unsorted element.
• Performs lesser number of swaps to sort the same array relative to the bubble sort.
• Selection sort is faster.
Bubble sort :
• Bubble sort compares adjacent elements and swaps them if they are in wrong order.
• Performs more number of swaps to sort the array.
• Bubble sort is slower.
Question 7
length and length()
length :
• length is an attribute i.e. a data member of array.
• It gives the length of an array i.e. the number of elements stored in an array.
length() :
• length() is a member method of String class.
• It gives the number of characters present in a string.
Page 10 of 11
ARRAY
• Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
• A dimensional array is a structure created in the memory to represent a number of values of the
same data type with the variables having the same variable name along with different
subscripts.
• A dimensional array is also called Subscripted Variable.
Creating a Single Dimensional Array (SDA)
Syntax:
<Data type><variable>[ ]=new <Data type>[<number of values to be stored>]
Example:
int m[ ] = new int [10]
(here, m is an array of integer data type & it can store 10 data items in the memory.)
Using Array Elements
Syntax:
<array name>[<subscript>]
Example:
m[1]
here, mis the subscripted variable (array name)
1 is the subscript.
Ways to input values to an array
•By using Assignment Statement
•By using Function argument (BlueJSystem)
•By using InputStreamReader class
•By using Scanner class
By using Assignment Statement
• Data values defined within the program
ex: int m[ ]={2,3,4,6,7}
• The length of the array will be the number of values initialized during declaration here, the length
of the array is 5. (here, m[5 or above] will result in Check for index out of bounds value error.)
• Array with empty [ ] represents an unfixed number of cells in the memory.
By usingFunction argument (BlueJSystem) :
• Data values to be entered at the time of executing the program
ex: public staticvoidmain(charm[ ])
• Use length function to find the number of values stored in an array
ex: int l= m.length;
• DuringtheMethod call execution , enter thearrayvalues inside the
curlybraces { }
Page 11 of 11
ex: {‘a’, ‘b’, ‘c’}
Download