search
HomeJavaHow to generate a specified range of 11-digit serial numbers?

Question content

I am using the given code to generate serial numbers in a list view up to a specific range of 11 digits. Up to ten digits, the generated sequence is like 0333624140 to 0333624160 (e.g. 0333624140, 0333624141, 0333624142, 0333624143 etc.) but if we change the required value to 11 digits like 03336241441 to 03336241450 then Application crashes.

btngenerate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int value1;
        int value2;
        value1 = Integer.parseInt(txtfirst.getText().toString());
        value2 = Integer.parseInt(txtsecond.getText().toString());
        for(int i=value1;i<=value2;i++){
            list.add(""+i);
            li.setAdapter(arrayAdapter);
        }

    }
});

Solution

I figured out the solution I needed on my own and with everyone's help. I'm showing it here so others can get information about it. Anyway, thank you from the bottom of my heart~

btngenerate.setOnClickListener(new View.OnClickListener() {
    @SuppressLint("DefaultLocale")
    @Override
    public void onClick(View view) {
        try {
            long value1;
            long value2;
            value1 = Long.parseLong(txtfirst.getText().toString());
            value2 = Long.parseLong(txtsecond.getText().toString());

            for(Long i = (Long) value1; i<=value2; i++) {
                list.add(String.format("%011d", i));
                li.setAdapter(arrayAdapter);
            }

        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
});

This is a completely broken idea as to how to write this code. You are creating an array that can contain every number that is 11 digits long. That’s 11 billion items. Even if each object only takes up 8 bytes (4 for the value and 4 for the reference. It would be more than that), that's 88 billion bytes, or about 9.5 gb. That will never fit in the memory, before that you'll be oom way.

There are many ways to do this. Your adapter doesn't need an array containing every item in it, since it's a contiguous range, it only needs the start and end index, and the value of each index can be calculated based on the start index and offset. But having a list of every value doesn't work.

The above is the detailed content of How to generate a specified range of 11-digit serial numbers?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment