Share on Google+Share on Google+

jaid.sk
merging
1 Answer(s)      7 years ago
Posted in : Java Beginners
how to merge two arrays and displaying the third array

Ads

View Answers

April 27, 2010 at 10:13 AM


Hi Friend,

Try the following code:

import java.util.*;
class MergeArray{
public static int[] merge(int []... arr) {
int arrSize = 0;
for (int[] array : arr) {
arrSize += array.length;
}
int[] result = new int[arrSize];
int j = 0;
for (int[] array : arr) {
for (int s : array) {
result[j++] = s;
}
}
return result;
}
public static void main(String[] args) {
int[] array1 = new int[5];
System.out.println("Enter 5 numbers");
Scanner input = new Scanner(System.in);
for (int i = 0; i < array1.length; i++) {
array1[i] = input.nextInt();
}
int[] array2 = new int[5];
System.out.println("Enter 5 numbers");
for (int i = 0; i < array2.length; i++) {
array2[i] = input.nextInt();
}
int M[] = (int [])merge(array1,array2);
System.out.println("Merged Array is: ");
for(int i=0;i<M.length;i++){
System.out.println(M[i]);
}

}
}

Thanks









Related Tutorials/Questions & Answers:
Merging Two Arrays Of Different Lengths
Merging Two Arrays Of Different Lengths  I have two arrays of different lengths and wants to have merged values into third. The only condition is, I want unique values in it(third array). Thanks In Advance
Merging multiple PDF files - Framework
Merging multiple PDF files  I m using the iText package to merge pdf files. Its working fine but on each corner of the merged filep there is some... the files are having different font.Please help
Advertisements
Merging of java script with servlet program
Merging of java script with servlet program  how can we merge the java script with the any servlet program for making web application intractive.Is there any program you have.\please help me to sove out this problem
Spring Collection Merging
Spring Collection Merging The merging of collection is allowed in the Spring 2.0. A parent-style like <list/>, <map/>, <set/> or <pros... collection. For collection merging you need to specify merge=true attribute
Merging Two Cells
Merging two cells       In this program we are going to merge two cells of an excel sheet... are merging the two cells into a single cell. In this example, Region(1,(short)1,1
EMFScript
(composition, merging, audit, ...). For these manipulations, transformation languages
What is EAI?
What is EAI?       EAI stands for Enterprise Application Integration. EAI works for merging the various project application into a common programming package that can be access
Merge two list into a single list
*mergelist (struct node *p, struct node *q) { struct node *r=NULL,*temp; if (p == NULL) r = q; else if(q == NULL) r = p; else { if (p->data < q->data ) { r = p; temp = p; p = p->link; temp->
Combining records from two or more orders files into a single ordered file is called
Combining records from two or more orders files into a single ordered file is called  Combining records from two or more orders files into a single ordered file is called? 1. Menu 2. Merging 3. Taking 4. All of the above 5
arrays in java - Java Beginners
arrays in java  Hi All, I have two arrays. in this two array some name are same. I want to merge those arrays into single. But while merging I want to delete duplicate entries. How merge those arrays. Thanks, mln15584
merge tables in sql using union
merge tables in sql using union  How to create a new table in database by merging two tables using union function in SQL?   REATE TABLE new_table SELECT * FROM table1 UNION SELECT * FROM table2;   
Database replication
. Following ways database replications are done: a) Snapshot replication b) Merging
PHP Array Merge Recursive
); echo"<br/><b>After merging two arrays the values of \$array1...; string(8) "diligent" } After merging two arrays the values... merging two arrays the values of \$array1 is:</b><br/>"
How to use dollar sign in velocity
): Merge method of the Template class is used here for merging
arrays
++) { System.out.println(tempArray1[i]); } //Merging array list...(tempArray4[i]); } //Merging array list tempArray3
What is JPA 2.1?
; JPA 2.1 user cab use specified fetching or merging of objects
including index in java regular expression
including index in java regular expression  Hi, I am using java regular expression to merge using underscore consecutive capatalized words e.g., "New York" (after merging "New_York") or words that has accented characters
How to define variable in Velocity
, writer): Merge method of the Template class is used here for merging the 
How to set a variable value using velocity
for merging the VelocityContext class object to produce the output
Foreach loop with negative index in velocity
here for merging the VelocityContext class object to produce the output
How To Remove Array Element In Java
;); } System.out.println(""); //Merging array list tempArray2 and tempArray1...;); } System.out.println(""); //Merging array list tempArray3 and tempArray4
Mysql Merge Statement
to create table named Emptotal which is the merging of both the table name...' that is the outcome merging of two table 'employee01' and 'employee02'. mysql> CREATE TABLE
Mysql Merge
named Emptotal which is the merging of both the table name employee01 and employee02 : The Query create a table 'Emptotal' that is the outcome merging of two
Mysql Merge
is the merging of both the table name employee01 and employee02 : The Query create a table 'Emptotal' that is the outcome merging of two table 'employee01
Mysql Merge Statement
(0.00 sec) Query to create table named Emptotal which is the merging... 'Emptotal' that is the outcome merging of two table 'employee01
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads