English 中文(简体)
我如何优化这一方法,以打破草坪的扼杀?
原标题:
  • 时间:2009-05-21 08:56:12
  •  标签:

此处采用这种方法。 我想知道,我是否在违反这方面的任何最佳做法,或者在语言方面,我是否做过错。

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;
    }
最佳回答

简言之,避免对清单进行可能的重新编制。

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;
}
问题回答

除非我误解你的意图,否则,它似乎像是一种巨大的超技能,并且利用大量时间扼杀,使算法在 j中的效率相当低,因为扼杀是不可调和的。

为此:

public List<String> breakStringsInChunks(String text,int chunkSize) {
    if (chunkSize<=1) {
        throw new IllegalArgumentException("Chunk size must be positive");
    }
    if (text==null || text.isEmpty()) {
        return Collections.emptyList();
    }

    List<String> chunks= new LinkedList<String>();

    int index=0;
    int len = text.length();

    //guaranteed to succeed at least once since 0 length strings we re taken care of
    do {
        chunks.add(text.substring(index, Math.min(index + chunkSize, len)));
        index+=chunkSize;
    } while (index<len);

    return chunks;
}

这是一种比喻,没有必要在你的方法开始时宣布<>临时,这样就可以使垃圾收集工作比较少。 下面简要介绍:

private List<String> breakStringInChunks(String text, int chunkSize) {
    int nChunks = (int)Math.ceil(((double)text.length())/chunkSize));
    List<String> chunks = new ArrayList<String>(nChunks);
    // Cut strings and add in the list.
    for (int i = 0; i < text.length(); i+=chunkSize) {
        int endIndex=i+chunksize;
        if (endIndex >= text.length()) {
            chunks.add(text.substring(i));
        } else {
            chunks.add(text.substring(i, endIndex));
        }
    }
    return chunks;
}

你的方法及以上案文有一个好的东西是,由于你总是在原封顶上称呼(), Java将只提及原来的特性阵列,从而节省了你们的一些记忆分配。

我想} 否则{是 Java更为常见的编码标准。

public static final List<String> chunk(final String text, final int chunkSize) {
    // Figure out how many chunks we are going to make.
    final int textLength = text.length();
    final int numberOfChunks =
        textLength % chunkSize == 0
        ? textLength / chunkSize
        : textLength / chunkSize + 1;

    // Create an array list of just the right size.
    final ArrayList<String> chunks = new ArrayList<String>(numberOfChunks);

    // Do all the chunking but the last one - here we know that all chunks
    // are exactly chunkSize long.
    for (int i = 0; i < numberOfChunks - 1; i++) {
        chunks.add(text.substring(i * chunkSize, (i + 1) * chunkSize));
    }

    // Add final chunk, which may be shorter than chunkSize, so we use textLength
    // as the end index.
    chunks.add(text.substring((numberOfChunks - 1) * chunkSize, textLength));

    return chunks;
}

这就是我的解决办法。 我努力实现这一目标,非常高效:

public static List<String> breakStringInChunks(String text, int chunkSize) {
    if (chunkSize < 2) {
        throw new IllegalArgumentException("Chunk size must be > 1");
    }
    if (null == text || text.isEmpty()) {
        return Collections.emptyList();
    }

    List<String> chunks = new ArrayList<String>(1 + (text.length() / chunkSize));

    int length = text.length() - (text.length() % chunkSize);

    for (int i = 0; i < length;) {
        chunks.add(text.substring(i, i += chunkSize));
    }
    if (length < text.length())
        chunks.add(text.substring(length));

    return chunks;
}

怎样做这样的事情?

private List<String> breakStringInChunks(String text, int chunkSize)
{
    List<String> chunks = new ArrayList<String>();
    while (text.length() > 0)
    {
        if (chunkSize > text.length())
        {
            chunkSize = text.length();
        }
        chunks.add(text.substring(0, chunkSize));
        text = text.substring(chunkSize);
    }
    return chunks;
}

这里就座。 与其他一些答复没有多大区别,但受考验,疲劳。

public class ChunkTest extends TestCase {
    public void testEmpty() throws Exception {
        assertEquals(0, breakStringInChunks("", 1).size());
    }

    public void testOneChunk() throws Exception {
        String s = "abc";
        List<String> chunks = breakStringInChunks(s, s.length());
        assertEquals(s, chunks.get(0));
        assertEquals(1, chunks.size());
    }

    public void testPartialChunk() throws Exception {
        String s = "abc";
        List<String> chunks = breakStringInChunks(s, s.length() + 1);
        assertEquals(s, chunks.get(0));
        assertEquals(1, chunks.size());
    }

    public void testTwoChunks() throws Exception {
        String s = "abc";
        List<String> chunks = breakStringInChunks(s, 2);
        assertEquals("ab", chunks.get(0));
        assertEquals("c", chunks.get(1));
        assertEquals(2, chunks.size());
    }

    public void testTwoEvenChunks() throws Exception {
        String s = "abcd";
        List<String> chunks = breakStringInChunks(s, 2);
        assertEquals("ab", chunks.get(0));
        assertEquals("cd", chunks.get(1));
    }

    private List<String> breakStringInChunks(String text, int chunkSize) {
        if (text.isEmpty())
            return Collections.emptyList();
        int n = (text.length() + chunkSize - 1) / chunkSize;
        List<String> chunks = new ArrayList<String>(n);
        for (int i = 0; i < n; ++i)
            chunks.add(text.substring(i * chunkSize, Math.min((i + 1) * chunkSize, text.length())));
        return chunks;
    }
}




相关问题
热门标签