Home  >  Article  >  How to generate a specified range of 11-digit serial numbers?

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

王林
王林forward
2024-02-12 14:27:05719browse

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.com. If there is any infringement, please contact admin@php.cn delete