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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)