cancel
Showing results for 
Search instead for 
Did you mean: 

Add element in array

Former Member
0 Kudos

Hi,

I have one String array.

I need to create another String array with the content of 1st array and additional 1st element in the 2nd array with "Product" text.

2nd array[0] = Product;

How do i do it

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hey

The System class has a arraycopy method which you can use

System.arraycopy();

Please look in Google for different parameters which are required for this.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

Thanks

Aamir

Edited by: Aamir Suhail on Jul 31, 2009 11:45 AM

Answers (2)

Answers (2)

Former Member
0 Kudos

String[] array1 = {"1", "2", "3"};

String[] array2 = {"4", "5"};

String[] array3 = new String[array1.length+array2.length];

System.arraycopy(array1, 0, array3, 0, array1.length);

System.arraycopy(array2, 0, array3, array1.length, array2.length);

here, the array3 will have all the elements in order ie, 1,2,3,4,5.

In case you need to use array2, assign array3 to array2 ie, array2 = array3

Hope that helps.

Regards,

Sumant

Former Member
0 Kudos

Hi

Pls, try with the following code:


String[] array1 = {"1","2","3"};
String[] array2 = new String[array1.length + 1];
array2[0] = "Product";
for(int i = 0; i < array1.length ; i++)
     array2[i+1] = array1<i>;

Regards

Ivan

Former Member
0 Kudos

Hi.

I tried this code

String[] b = new String[a.length + 1];

b[0] = "Product";

for(int i = 0; i < a.length ; i++){

b[i+1] = a<i>;

result.addValue(b<i>);

}

I want to create 2nodes.

1st node = b[0]

2nd node = b[1]

Now only b[0] is getting created.

Thanks

Former Member
0 Kudos

Hi NPrabhu,

Have you checked the input array string? which value does it have?

Regards

Ivan

former_member187339
Active Contributor
0 Kudos

Hi Prabhu,

Check this modified code


String[] b = new String[a.length + 1];
b[0] = "Product";

for(int i = 1; i < a.length ; i++){
b[0] += a[i-1];
}

result.addValue(b[0]);

Is this what you required??

Regards

Suraj

Former Member
0 Kudos

Cool. I liked that :). Good implementation for UDF.