首页 > Java > java教程 > 正文

如何确定Java中数组的长度或大小?

WBOY
发布: 2023-09-17 22:05:07
转载
922人浏览过

如何确定java中数组的长度或大小?

In Java, one of the convenient ways to determine the length or size of an array is by using its length property. It counts the number of elements stored in an array and returns the count. Finding the length of an array is one of the common yet crucial operation as it is used to find the number of elements of an array, append new elements into it and retrieve stored items. This article aims to explain the various ways to get the length or size of an array.

Java Program to determine the length or size of an Array

以下示例将帮助我们了解如何找到数组的大小。

Example 1

的中文翻译为:

示例 1

The following example illustrates the use of length property with an array of type integer.

import java.util.*;
public class Example1 {
   public static void main(String[] args) {
      int[] aray = new int[5]; // declaring array of size 5
      // initializing the array
      aray[0] = 11;
      aray[1] = 21;
      aray[2] = 13;
      aray[3] = 23;
      aray[4] = 30;
      // printing the elements of array
	  System.out.println("Elements of the given array: ");
	  for(int elm : aray) {
	     System.out.print(elm + " "); 
	  }
	  System.out.println();
	  // printing the length of array
      System.out.println("Length of the given array: " + aray.length); 
   }
}   
登录后复制

Output

Elements of the given array: 
11 21 13 23 30 
Length of the given array: 5
登录后复制

在上面的代码中,我们声明了一个大小为5的数组,这意味着它可以存储最多5个元素。然后,使用for-each循环,我们检索了所有的元素,并且借助length属性确定了给定数组的大小。

立即学习Java免费学习笔记(深入)”;

Example 2

的中文翻译为:

示例2

In this example, we will declare and initialize a String array. Then, using a for-each loop, we will print its element. In the end, with the help length property, we determine the size of given array.

import java.util.*;
public class Example2 {
   public static void main(String[] args) {
      // declaration and initialization of the array
      String[] aray = { "Tutorix", "Tutorials", "Point", "Simply", "Easy",
         "Learning"            
         }; 
	  // printing the elements of array
	  System.out.println("Elements of the given array: ");
	  for(String elm : aray) {
	     System.out.print(elm + " "); 
      }
	  System.out.println();
	  // printing the length of array
      System.out.println("Length of the given array: " + aray.length); 
   }
}
登录后复制

Output

Elements of the given array: 
Tutorix Tutorials Point Simply Easy Learning 
Length of the given array: 6
登录后复制

Example 3

This is another example where we will find the size of an array without using any in-built property or method of Java.

方法

  • 声明并初始化两个数组。一个是字符串数组,另一个是整数数组

  • 然后,定义两个整数类型的计数变量来存储两个数组的元素数量。

  • Now, use the for-each loop to iterate and increment the counter variable by one with each iteration.

  • 最后,打印结果并退出。

import java.util.*;
public class Example3 {
   public static void main(String[] args) {
      // declaration and initialization of the arrays
      String[] aray1 = { "Tutorix", "Tutorials", "Point", "Simply", "Easy",
         "Learning"            
         }; 
      int[] aray2 = {58, 66, 74, 55, 62};
      // initial counter variable to store the count of elements
      int countr1 = 0;
      int countr2 = 0;
	  // printing the length of both arrays
	  for(String elm : aray1) {
	     countr1++;
	  }
	  for(int elm : aray2) {
	     countr2++;
	  }
      System.out.println("Length of the String array: " + countr1); 
      System.out.println("Length of the integer array: " + countr2); 
   }
}
登录后复制

Output

Length of the String array: 6
Length of the integer array: 5
登录后复制

结论

在本文中,我们通过示例程序学习了使用length属性来确定给定数组的大小。此外,我们还讨论了一种在不使用任何内置方法和属性的情况下找到数组长度的方法。

以上就是如何确定Java中数组的长度或大小?的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号