banner



how to find length of array in java

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Constructive Resume Writing
  • Hr Interview Questions
  • Estimator Glossary
  • Who is Who

How do I detect the length of an array in C/C++?


Some of the methods to find the length of an assortment are given as follows −

Method 1 - Using sizeof operator

The sizeof() operator tin can be used to find the length of an array. A programme that demonstrates the apply of the sizeof operator in C++ is given as follows.

Instance

 Live Demo

#include <iostream> using namespace std; int main() {    int arr[5] = {4, ane, eight, 2, 9};    int len = sizeof(arr)/sizeof(arr[0]);    cout << "The length of the array is: " << len;    render 0; }

The output of the above program is as follows −

The length of the array is: 5

Now, let us empathise the above program.

The variable len stores the length of the array. The length is calculated by finding size of array using sizeof and and so dividing it by size of one element of the assortment. Then the value of len is displayed. The lawmaking snippet for this is given as follows −

int arr[5] = {iv, ane, viii, 2, nine}; int len = sizeof(arr)/sizeof(arr[0]); cout << "The length of the array is: " << len;

Method ii - Using pointers

Arrow arithmetic can be used to notice the length of an assortment. A programme that demonstrates this is given as follows.

Instance

 Live Demo

#include <iostream> using namespace std; int main() {    int arr[five] = {5, 8, 1, 3, half dozen};    int len = *(&arr + 1) - arr;    cout << "The length of the assortment is: " << len;    return 0; }

Output

The output of the to a higher place program is every bit follows −

The length of the array is: v

Now, permit us understand the higher up programme.

The value independent in *(&arr + 1) is the address subsequently 5 elements in the array. The value contained in arr is the address of the starting element in array. And then their subtraction results in the length of the array. The code snippet for this is given as follows −

int arr[five] = {5, 8, 1, three, vi}; int len = *(&arr + 1) - arr; cout << "The length of the array is: " << len;

raja

Published on 21-Nov-2018 11:48:41

  • Related Questions & Answers
  • How do you find the length of an array in C#?
  • How do yous find the number of dimensions of an array in C#?
  • How exercise I determine the size of my array in C#
  • How to find the length of an assortment in JavaScript?
  • Find the Derangement of An Assortment in C++
  • How to find the length of the longest continuous increasing subsequence from an array of numbers using C#?
  • How to find the length and rank of a jagged array in C#?
  • How to find the length of the StringBuilder in C#?
  • Find the length of each string element in the Numpy array in C++
  • How do I empty an assortment in JavaScript?
  • Where practice I find the current C or C++ standard documents?
  • How practice you empty an array in C#?
  • How exercise I use the conditional operator in C/C++?
  • How practise I sort a ii-dimensional array in C#
  • How do I overload the [] operator in C#?

Source: https://www.tutorialspoint.com/how-do-i-find-the-length-of-an-array-in-c-cplusplus

Posted by: benoithoughle.blogspot.com

0 Response to "how to find length of array in java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel