Hello, guys today we would be performing basic array operations in java in eclipse IDE order to understand the use and importance of array in java the basic array operations are mentioned below:
大家好,今天我们将在Eclipse IDE中使用Java执行基本的数组操作,以了解Java中数组的用法和重要性,下面将介绍基本的数组操作:
Creating an array.
创建一个数组。
Entering values into the array.
在数组中输入值。
Finding the maximum value from the array.
从数组中找到最大值。
Finding the minimum value from the array.
从数组中找到最小值。
Searching a particular value in the array using linear search.
使用线性搜索在数组中搜索特定值。
Sorting the array using bubble sorting.
使用冒泡排序对数组排序。
Below is the self-explanatory java code is written in eclipse IDE to help you understand the basic operations of arrays in java.
以下是用eclipse IDE编写的不言自明的Java代码,以帮助您了解Java中数组的基本操作。
import java.util.*;
class Codechef
{
public static void main(String[] args)
{
int c=0,k;
//creating an object of scanner class for taking input from the user
Scanner Sc= new Scanner(System.in);
System.out.println("Enter the number of elements you want in your array");
//taking the length of the array from the user using nextInt() method
int len=Sc.nextInt();
//creating an array from the user defined length
int arr[]=new int[len];
System.out.println("Enter the elements in the array");
//taking input from the user using for loop
for(int i=0;i<len;i++)
{
//nextInt() method allows the user to enter the values inside the array
arr[i]=Sc.nextInt();
}
// temp variable is used to perform sorting of the variables in the bubble sorting
int temp=0;
//using nested loops for bubble sort to sort elements in ascending order
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(arr[i]>arr[j])
{temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;}
}
}
System.out.println("The largest element in the array is ");
//since the elements are sorted in ascending order
//the last element is the largest element
System.out.println(arr[len-1]);
System.out.println("The smallest element in the array is");
//this will print the smallest element
System.out.println(arr[0]);
System.out.println("Enter the element you want to search");
//taking the input from the user to perform linear search
int g=Sc.nextInt();
//for loop to check whether the given element is present in the array or not
for(int i=0;i<len;i++)
{
if(g==arr[i])
{
//to show the actual indexing since the indexing of array starts from 0
k=i+1;
System.out.println("Element found at "+ k+"th " +"position");
c=1;
}
}//incrementing the value of c variable in case the element is found
if(c!=1)// if the element is not found
System.out.println("Element not found");
System.out.println("elements in the sorted order");
// for loop to print the list of elements
//that we have sorted using bubble sort
for(int i=0;i<len;i++)
{
//print statement to print the elements of the array
System.out.println(arr[i]);
}
}
}
Output
输出量
Enter the number of elements you want in your array
6
Enter the elements in the array
6
1
8
7
6
4
5
The largest element in the array is
8
The smallest element in the array is
1
Enter the element you want to search
1
Element found at 1th position
elements in the sorted order
1
4
5
6
7
8
Try experimenting with the code written above and make your own array related programs in Java, hope you like this article. Have a great day ahead and keep learning.
尝试尝试上面编写的代码,并使用Java编写与数组相关的程序 ,希望您喜欢本文。 祝您有美好的一天,继续学习。
翻译自: https://www.includehelp.com/java/basic-array-operations.aspx