Array length Calculation

Source: Internet
Author: User
Tags array length

In the C language, there is the sizeof () keyword . Can be used to calculate the size of an array (the number of bytes in the array), but not the length .

However, it can be divided by the number of bytes of an array of individual elements to calculate the length:

A[7] = {1,2,3,4,5,,6,7};

length = sizeof (a)/sizeof (a[0]);

The above method can be used in C + +, but you can also:

A[7] = {1,2,3,4,5,6,7}

int *abeg = Beg (a);

int *aend = end (a);

Auto dis = Aend-abeg;

cout<<dis<<endl;

In C + +, it is recommended to use vector containers instead of arrays, whose lengths can be calculated directly using Name.size ().

Calculate the size of the array in Java:

public class Testjava {
public static void Main (string[] args) {
int a[] = {1,2,3,4,5};
System.out.println (a.length);
}
}

Calculate the size of the array in Python:

A = [1,2,3,4,5]

Print (Len (a))

Very simple things, but too long did not write code after unexpectedly still forget. It's really going to take more practice.

Array length calculation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.