1

Hi I use for loop to add the strings which is in array. Can anyone help me for the below code it shows error.

image = new String[] {"APP","FIELD","KYC"};

image2 = new String[] {"MEMORANDUM","ASSOCIATION"};

Now using for loop or any method I need the same image array as

image = new String[] {"APP","FIELD","KYC","MEMORANDUM","ASSOCIATION"};
Hybrid Developer
  • 2,320
  • 1
  • 34
  • 55
Manju
  • 65
  • 3
  • 11
  • what is the error? show us your for loop – sanbhat Aug 06 '13 at 07:46
  • 1
    The code you posted should not show any error. There is a number of ways to achieve what you are trying to achieve, with or without loops. – Mena Aug 06 '13 at 07:48

4 Answers4

6

Yes this can be possible without loop. Use ArrayUtils.addAll(T[], T...)

String[] both = ArrayUtils.addAll(image, image2);

Here is a solution with array to/from List conversions.

String[] image = new String[] {"APP","FIELD","KYC"};
String[] image2 = new String[] {"MEMORANDUM","ASSOCIATION"};
List<String> list = new ArrayList<String>(Arrays.asList(image));
list.addAll(Arrays.asList(image2));
String[] result = list.toArray(new String[]{});
System.out.println(Arrays.toString(result));

Output will be the same as you required in question.


As Mena suggested another solution can be System.arraycopy

String[] image = new String[] {"APP","FIELD","KYC"};
String[] image2 = new String[] {"MEMORANDUM","ASSOCIATION"};
String[] result = new String[image.length + image2.length]; 

// copies an array from the specified source array
System.arraycopy(image, 0, result, 0, image.length);
System.arraycopy(image2, 0, result, image.length, image2.length);

// Now you can use result for final array

Read more about How can I concatenate two arrays in Java?

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • 1
    You should specify this requires Apache Commons. – Mena Aug 06 '13 at 07:49
  • @Mena I added the link. – Pankaj Kumar Aug 06 '13 at 07:49
  • Ah yes just seen the edit now. Fair enough. Although I would be even more explicit about the fact that your solution requires a dependency in the project (and there are alternate solutions that don't). – Mena Aug 06 '13 at 07:51
  • @Mena solution added and updated duplicate link. – Pankaj Kumar Aug 06 '13 at 07:55
  • Better ;) You've got my +1. Could have used [arrayCopy](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int)) as well for this. – Mena Aug 06 '13 at 08:00
  • @Mena Hey you were right.. I check the method and its working for the purpose.. thanks for introducing me with a nice method. – Pankaj Kumar Aug 06 '13 at 08:16
0

Use the following code

    ArrayList<String> image = new ArrayList<String>();
    image.add("APP");
    image.add("FIELD");
    image.add("KYC");

    ArrayList<String> image2 = new ArrayList<String>();
    image2.add("MEMORANDUM");
    image2.add("ASSOCIATION");

    image.addAll(image2);

    for(String i:image){
        System.out.println(i);
    }
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

Java by default do not has such util. What you can do is to use System.arraycopy

public static void copy(Object[] a, Object... b) {

  int alen = a.length;
  int blen = b.length;

  Object[] c = new Object[alen + blen);

  System.arraycopy(a, 0, c, 0, alen);
  System.arraycopy(b, 0, c, alen, blen);

  return c;
}

You may wish to use the generics

public static <T> void copy(T[] a, T... b) {

  int alen = a.length;
  int blen = b.length;

  T[] c = (T[]) Array.newInstance(a.getClass(),alen + blen);

  System.arraycopy(a, 0, c, 0, alen);
  System.arraycopy(b, 0, c, alen, blen);

  return c;
}

But all this is required if you are really attached to Arrays, but it seams that you change the size during program flow. Reconsider using a List<String> that will allow you to store strings and extend it frellly.

0
public static void main(String[] args) {
    String[] image = new String[] { "APP", "FIELD", "KYC" };
    String[] image2 = new String[] { "MEMORANDUM", "ASSOCIATION" };     

    String[] image3 = new String[image.length+image2.length];       

    for (int i = 0; i <image.length; i++) {
        image3[i] = image[i];

    }

    for (int i = image.length; i>=0 && i < image3.length; i++) {
        image3[i] = image2[i-3];
    }


    //Check if image3 contains the elements you need.
    for(String imageData:image3)
    {
        System.out.println(imageData);
    }

}

This is not a generic solution. It just solves what you need. Can you elaborate more on what are you trying to do?

rickygrimes
  • 2,637
  • 9
  • 46
  • 69