如何优化这一方法打破了字符串块? [英] How do I optimize this method for breaking a string in chunks?

查看:136
本文介绍了如何优化这一方法打破了字符串块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的方法。我想知道,如果我在这里没有违反任何的最佳做法或如果我做错事就语言而言。

 私人列表<字符串> breakStringInChunks(字符串文本,诠释CHUNKSIZE){
        名单<字符串>块=新的ArrayList<字符串>();
        字符串临时=;
        INT numberOfChunks = text.length()/ CHUNKSIZE;
        INT beginIndex = 0;
        INT endIndex的= 0;

        //添加一个迭代,如果numberOfChunks * CHUNKSIZE小于文本长度。
        如果((numberOfChunks * CHUNKSIZE)所述; text.length()){
            numberOfChunks ++;
        }

        //切串,并在列表中添加。
        的for(int i = 0; I< numberOfChunks;我++){
            endIndex的+ = CHUNKSIZE;
            如果(第(i + 1)== numberOfChunks){
                临时= text.substring(beginIndex);
            }
            其他 {
                临时= text.substring(beginIndex,endIndex的);
            }
            beginIndex = endIndex的;
            chunks.add(临时);
        }

        返回块;
    }
 

解决方案

更​​加简要的是,避免了结果列表中的潜力大小调整。

 私有静态列表<字符串> breakStringInChunks(最终文本字符串,最终诠释CHUNKSIZE){
    最终诠释NUMCHUNKS = 0 ==(text.length()%CHUNKSIZE)? text.length()/ CHUNKSIZE:1 +(text.length()/ CHUNKSIZE);
    最后的名单,其中,字符串>块=新的ArrayList<字符串>(NUMCHUNKS);
    对于(INT的startIndex = 0;在startIndex< text.length();在startIndex + = CHUNKSIZE){
        最终诠释endIndex的= Math.min(text.length(),在startIndex + CHUNKSIZE);
        chunks.add(text.substring(在startIndex,endIndex的));
    }
    返回块;
}
 

Here's the method. I want to know if I am violating any best practices here or if I am doing something wrong as far as the language is concerned.

private List<String> breakStringInChunks(String text, int chunkSize) {
        List<String> chunks = new ArrayList<String>();
        String temporary = "";
        int numberOfChunks = text.length() / chunkSize;
        int beginIndex = 0;
        int endIndex = 0;

        // Add one iteration if numberOfChunks*chunkSize is less than the length of text.
        if ((numberOfChunks * chunkSize) < text.length()) {
            numberOfChunks++;
        }

        // Cut strings and add in the list.
        for (int i = 0; i < numberOfChunks; i++) {
            endIndex+=chunkSize;
            if ((i + 1) == numberOfChunks) {
                temporary = text.substring(beginIndex);
            }
            else {
                temporary = text.substring(beginIndex, endIndex);
            }
            beginIndex=endIndex;
            chunks.add(temporary);
        }

        return chunks;
    }

解决方案

Briefer still, and avoids potential resizing of the resulting list.

private static List<String> breakStringInChunks(final String text, final int chunkSize) {
    final int numChunks = 0 == (text.length() % chunkSize) ? text.length() / chunkSize : 1 + (text.length() / chunkSize);
    final List<String> chunks = new ArrayList<String>(numChunks);
    for (int startIndex = 0; startIndex < text.length(); startIndex += chunkSize) {
        final int endIndex = Math.min(text.length(), startIndex + chunkSize);
        chunks.add(text.substring(startIndex, endIndex));
    }
    return chunks;
}

这篇关于如何优化这一方法打破了字符串块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆