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属性确定了给定数组的大小。
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中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

Dreamweaver Mac版
视觉化网页开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。