优化字符串操作代码

时间:2014-01-21 16:59:08

标签: java string

我正在尝试从给定主机中获取所有可能的子域。我写了以下代码。它有效但我唯一担心的是性能。

是否需要进一步优化此代码:

import java.util.Arrays;
import java.util.Collections;

public class DemoExtractHostArray {
    public static  String[] createHostArray(String host) {
        String[] stringArr = host.split("\\.");
        String[] hostArray = new String[stringArr.length];
        int hostIndex = 0;

        for(int index = stringArr.length-1; index>=0;index--){
            if(hostIndex==0){ 
                hostArray[hostIndex] = stringArr[index];
            }
            else{
                hostArray[hostIndex] = stringArr[index]+"."+hostArray[hostIndex-1];
            }
            hostIndex++;
        }
        Collections.reverse(Arrays.asList(hostArray));
        return hostArray;
    }

    public static void main(String a[]){
        for(String s: createHostArray("a.b.c.d.e.f")){
            System.out.println(s);
        }

    }
}

输出:

a.b.c.d.e.f
b.c.d.e.f
c.d.e.f
d.e.f
e.f
f

3 个答案:

答案 0 :(得分:1)

您的代码唯一可能的改进是删除此调用:

Collections.reverse(Arrays.asList(hostArray));

由于您正在创建hostArray然后将其反转,您可以更改循环以立即以相反的顺序创建数组,以便不再需要显式反转:

// hostIndex is no longer required - remove the line below:
// int hostIndex = 0;
for(int index = stringArr.length-1 ; index>=0 ; index--){
    if(index == stringArr.length-1) {
        hostArray[index] = stringArr[index];
    }
    else{
        hostArray[index] = stringArr[index]+"."+hostArray[index+1];
    }
}

答案 1 :(得分:0)

我个人会使用妄想。此实现不需要反转数组,在我看来,可能更容易遵循。

http://ideone.com/MnMZOL

package com.poachit.utility.web;

import java.util.Arrays;
import java.util.Collections;

public class DemoExtractHostArray {
     public static void createHostArray(String[] root, String[] result, int index) {

        String host="";
        int i = index;
        if (index == root.length) {
            return;
        }
        for ( ; i < root.length-1; i++) {           
            host += root[i] + ".";
        }

        if (i < root.length) {
            host += root[i];
        }

        result[index] = host;
        createHostArray(root, result, ++index);
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        String host = "a.b.c.d.e.f";

        String [] tokens = host.split("\\.");

        String [] result = new String[tokens.length];

      createHostArray(tokens, result, 0);

      for (String s : result) {
        System.out.println(s);
      }

    }
}

答案 2 :(得分:0)

您可以像这样优化代码

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test {
    public static  String[] createHostArray(String host) {
        String[] stringArr = host.split("\\.");
        String[] hostArray = new String[stringArr.length];
        int hostIndex = 0;

        for(int index = stringArr.length-1; index>=0;index--){
            if(hostIndex==0){ 
                hostArray[hostIndex] = stringArr[index];
            }
            else{
                hostArray[hostIndex] = stringArr[index]+"."+hostArray[hostIndex-1];
            }
            hostIndex++;
        }
        Collections.reverse(Arrays.asList(hostArray));
        return hostArray;
    }

    public static  String[] betterCreateHostArray(String host) {
        List<String> hostList = new ArrayList<String>();
        do {
            if(!host.contains(".")) {
                hostList.add(host);
                break;
            } else {
                hostList.add(host);
                host = host.substring(host.indexOf('.')+1);
            }
        } while(host.length() > 0);

        return hostList.toArray(new String[hostList.size()]);
    }

    public static void main(String a[]){
        long startTime = System.nanoTime();
        String[] array = createHostArray("a.b.c.d.e.f");
        long endTime = System.nanoTime();
        long timeByFirstApproach = endTime - startTime;

        for(String s: array){
            System.out.println(s);
        }
        System.out.println("=====");
        startTime = System.nanoTime();
        array = betterCreateHostArray("a.b.c.d.e.f");
        endTime = System.nanoTime();
        long timeBySecondApproach = endTime - startTime;
        for(String s: array){
            System.out.println(s);
        }
        System.out.println(String.format("Time taken by first approach=[%d] nano seconds and\n"
                + "Time taken by second approach=[%d] nano seconds", timeByFirstApproach,timeBySecondApproach));
    }
}

这里是绩效结果

a.b.c.d.e.f
b.c.d.e.f
c.d.e.f
d.e.f
E.F
˚F
=====
a.b.c.d.e.f
b.c.d.e.f
c.d.e.f
d.e.f
E.F
˚F
第一次逼近所花费的时间= [1625572]纳秒秒和 第二种方法所花费的时间= [308289]纳秒
第二种方法比您所遵循的方法快5倍以上。