1

This code gives me an output of -

first_name- [Tiger, Enrique]

family_name- [Woods, Iglesias]

import java.util.*;

public class al_Concatenate {
public static void main(String args[]) {

  // creating array list
  ArrayList<String> first_name  = new ArrayList<String>();
  ArrayList<String> family_name = new ArrayList<String>();
  Arraylist<String> name        = new Arraylist<String>(); 
  // add elements to the array list
  first_name.add("Tiger");
  first_name.add("Enrique");
  family_name.add("Woods");
  family_name.add("Iglesias");

  name.??

  //displaying the output
  System.out.println("first_name- " + first_name);
  System.out.println("family_name- "+ family_name);
  System.out.println("name- "+  name);
   }
}

But I need and output like this (without using loop, preferably a single line code)-

name - [Tiger Woods, Enrique Iglesias]

thanks in advance

I would also like to add that the size of the arraylist is 2, but in my real program it's around 4500. I created this example to make it simple :) I edited the post a bit, sorry for making it cumbersome :(

3
  • 3
    fisrtName.get(0) + " " + family_name.get(0)...? Commented Nov 11, 2014 at 23:17
  • 2
    Is this for a class where the instructions are not to use a loop? If not, then why don't you want to use a loop? Unless you're using Java 8, there really isn't a way to do it without a loop, since there aren't any built-in Java methods to combine two ArrayLists in the way you describe. Commented Nov 11, 2014 at 23:19
  • 1
    P.S. Please don't use raw ArrayLists; the first declaration should be ArrayList<String> first_name = new ArrayList<>();. Commented Nov 11, 2014 at 23:32

7 Answers 7

1

Well, here's a one-liner for you (using Java 8), even if it is just silly:

System.out.println(IntStream.range(0, first_name.size()).mapToObj(n -> first_name.get(n) + " " + family_name.get(n)).collect(Collectors.toList()).toString());

I've tested this and it works (after a couple tries).

Explanation: IntStream.range creates a stream of integers, in order from 0 to first_name.size()-1 (this is a half-open range). mapToObj maps each integer n in that range to a string created by concatenating the strings from the two arrays, with a space in between. collect then creates a List of all the strings created by mapToObj. The toString() method of the List puts the strings in square brackets separated by commas, as in the original question. (I could have left out the toString() since println would have done that automatically.)

P.S. I don't know that this solution is actually silly--that was a joking reference to a previous answer. Functional programming fans will probably think this is a much better way to do things. Personally, I'm undecided about which is more readable, but this kind of solution may take some getting used to if you're not already familiar with FP.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ajb. It worked for me, I wanted this. But I wanted to ask you why did you say that this method is silly ?
@ArnabSarkar reference to ilia_b's answer.
if it's a sarcastic comment, it's a good one :P because one line command always saves a lot of processing time, when working with big data :)
0

a loop over the array elements is really the way to go here. you could potentially use Java8 Streams to manage this without a loop but it would be just silly.

2 Comments

One line command always saves a lot of processing time, when working with big data :)
4500 entries is not really big data in my opinion. worrying about runtime performance seems like premature optimization in this case. the simplest solution to implement is probably good enough.
0

You must use for loop. You cannot traverse array without loop.

for(int i = 0; i<Math.min(first_name.size(), family_name.size()); i++) {
            System.out.println(first_name.get(i) + " " + family_name.get(i));
}

Comments

0

You could use a map instead of 2 ArrayLists? You would get something like:

Map<String, String> names = new HashMap<String, String>();
names.put("Tiger", "Woods");
names.put("Enrique", "Iglesias");

System.out.println("name - " + names);

Output would be:

name - {Tiger=Woods, Enrique=Iglesias}

Would it fit your needs?

Comments

0

Try this for your print statement:

System.out.println("name - [" + first_name.get(0) + " " + family_name.get(0) + ", " + first_name.get(1) + " " + family_name.get(1) + "]");

Does that work?

Comments

0

Recursion can work for this case, if no-loop is needed:

static void print(List l1, List l2) {
    if(!l1.isEmpty()) {
        System.out.println("first_name: " + l1.get(0) + "; family_name: " + l2.get(0));
        print(l1.subList(1, l1.size()), l2.subList(1, l2.size()));
    }
}

Comments

0

Instead of using:

System.out.println();

Using the code below won't skip lines after printing:

System.out.print()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.