0

I was trying to do something like:

ArrayList<String> getMerged ( String host, String port, String filesToCopy ){
  ArrayList<String> merged = new ArrayList<String>();
  merged.add(host);
  merged.add(port);
  merged.addAll(filesToCopy.split(","));   //which is invalid
  return merged;
}

I want to know if we can add elements of filesToCopy.split(",") with out having the overhead of using a loop.

Also, if the above operation can be done in a string array, say String[] merged (can pass filesToCopy also as String[] if needed), it would be even better coz in the end, I'll be converting this arrayList into an array.

I'm novice in Java programming, so please don't mind if this is a silly question.

human
  • 168
  • 1
  • 10
  • 1
    Slightly confused. `filesToCopy` is a list, why would you need to split the strings up within it? Unless `filesToCopy` is meant to be a single string containing comma delimited file names? Maybe you should look into varargs arguments for methods.... – Dan Temple Mar 26 '14 at 13:13
  • I'm also not sure what you are wanting to do with the `split`. `filesToCopy` is an ArrayList so what is that supposed to hypothetically accomplish? Show us what you are trying to do *using the loop that you don't want to use*. – Radiodef Mar 26 '14 at 13:36
  • @DanTemple & Radiodef - I'm sorry, that was a typo; filesToCopy is a string. "(can pass filesToCopy also as String[] if needed)" - coz I can pass the arg as from caller as filesToCopy.split(","). – human Mar 27 '14 at 07:30
  • Well, for how you have the code now (provide `filesToCopy` as a String) you can just put `Arrays.asList(filesToCopy.split(",");` into the `addAll()` method and it should work fine. – Dan Temple Mar 27 '14 at 07:40
  • @Vnm i am not about sure what you trying to do.did you checked my answer.update the status if none of my methods are usefull... – Prakash Mar 27 '14 at 07:41
  • @Radiodef : By "using the loop that you don't want to use.", what I meant is, if there is some system call that we can make to do this, which will improve the performance (like memcopy or std::copy in C). – human Mar 27 '14 at 07:57
  • @Prakash, Yea, I saw the answer, was trying it out. Also I was checking if the ans given by you or Doorknob performs better. Will update soon.. Thanks for the reply :) – human Mar 27 '14 at 08:01
  • I got answer for performance part from the link: http://stackoverflow.com/questions/8526907/is-javas-system-arraycopy-efficient-for-small-arrays – human Mar 27 '14 at 10:39

3 Answers3

1

You could do this in a single array:

String[] files = filesToCopy.split(","); // filesToCopy is an ArrayList, so I'm not
                                         // sure how this works; I'm assuming it's
                                         // a typo. Just get the files array somehow
String[] merged = new String[2 + files.length];
merged[0] = host;
merged[1] = port;
for (int i = 2; i < merged.length; i++) {
    merged[i] = files[i-2];
}

Or, without "the overhead of a loop":

merged[0] = host;
merged[1] = port;
System.arraycopy(files, 0, merged, 2, files.length);

Of course, this still uses a loop "behind the scenes," which is unavoidable.

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • Yeah, that I understand, but I was wondering if it works better than using loop by ourselves. Like mem copy or sys::copy in C – human Mar 27 '14 at 08:03
  • I got my answer for performance part from the link: http://stackoverflow.com/questions/8526907/is-javas-system-arraycopy-efficient-for-small-arrays – human Mar 27 '14 at 10:37
1

ArrayList.addAll method requires a Collection as a parameter, so just pass the filesToCopy:

String [] getMerged ( String host, String port, ArrayList<String> filesToCopy ){
  ArrayList<String> merged = new ArrayList<String>();
  merged.add(host);
  merged.add(port);
  merged.addAll(filesToCopy); 
  return merged.toArray(new String[merged.size());
}

PS: I just a matter of opinion, but if I can choose between arrays and Collections, I always prefer to work with Collections (List, Set). Variable size and easy insertions are things to take into account.

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
1

I am not sure about what your need is.But i am sure anyone of the below methods will surely help you..

1.Covert String With Comma To A ArrayList

Program:

    import java.util.Arrays;
    ....  
    String name="java,php,c";
    List<String> list=Arrays.asList(name.split(","));
    System.out.println(" "+list);

OutPut:

    [java, php, c]

2.Covert ArrayList To StringArray

Here we can convert the same arraylist that we got in 1st method to string array.

Program:

    String []names=list.toArray(new String[list.size()]);
    for(String s:names){
            System.out.println(""+s);
    }

OutPut:

    java
    php
    c

3.Covert ArrayList To Comma Seperated String

Here we can convert the same arraylist that we got in 1st method to string array.

For this you need To add commons-lang3-3.2.1.jar into your classpath or project libarary.

You can Download The commons-lang3-3.2.1.jar (HERE)

Program:

    import org.apache.commons.lang3.StringUtils;
    .....
    String name=StringUtils.join(list, ",");
    System.out.println("name="+name);

OutPut:

    name=java,php,c

4.Updated Program

This might me the method that you needed

public String[] getMerged(String host, String port, String filesToCopy) {
    String files[] = filesToCopy.split(",");
    String[] merged = new String[(2 + files.length)];
    merged[0] = host;
    merged[1] = port;
    System.arraycopy(files, 0, merged, 2, files.length);
    return merged;
}

Check out these methods and notify me if your need is something other than these methods..

Prakash
  • 693
  • 5
  • 17
  • Hi Prakash, to the merged array, I need to two Strings (say host n port) first and then merge the contents of .split(",") to it. If split() was unnecessary, I could've used the method "Pablo Lozano" mentioned, which I knew already.. – human Mar 27 '14 at 10:36