Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top