使用Eclipse的Java中的基本阵列操作

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中数组的用法和重要性,下面将介绍基本的数组操作:

  1. Creating an array.

    创建一个数组。

  2. Entering values into the array.

    在数组中输入值。

  3. Finding the maximum value from the array.

    从数组中找到最大值。

  4. Finding the minimum value from the array.

    从数组中找到最小值。

  5. Searching a particular value in the array using linear search.

    使用线性搜索在数组中搜索特定值。

  6. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值