Java Data Structure - Programming Examples - Learn how to play with data structure in Java programming. Here are most commonly used examples −.
![]()
Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.
Array Representation
Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
As per the above illustration, following are the important points to be considered.
Basic Operations
Following are the basic operations supported by an array.
In C, when an array is initialized with size, then it assigns defaults values to its elements in following order.
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of the array −
Algorithm
Let Array be a linear unordered array of MAX elements.
Example
Result
Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA −
Example![]()
Following is the implementation of the above algorithm −
When we compile and execute the above program, it produces the following result −
Output
For other variations of array insertion operation click here
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kth position of LA.
Example
Following is the implementation of the above algorithm −
When we compile and execute the above program, it produces the following result −
OutputSearch Operation![]()
You can perform a search for an array element based on its value or its index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to find an element with a value of ITEM using sequential search.
Example
Following is the implementation of the above algorithm −
When we compile and execute the above program, it produces the following result −
OutputUpdate Operation
Update operation refers to updating an existing element from the array at a given index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to update an element available at the Kth position of LA.
Example
Following is the implementation of the above algorithm −
When we compile and execute the above program, it produces the following result −
Output![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |