Merge arrays of integers

Hi,
I hope someone can help me with a method which merges two arrays of intgers. The main idea is to
divide a array into two almost exact lenghts, sort those arrays and then merge the two sorted arrays.

int[] intArray;
int[] int_Array;
int lenIntArray=intArray.length;
int lenInt_Array=int_Array.length;
//Merged Array with length equal to the sum of arrays to be merged.
int[] mergedArray=new int[lenIntArray+lenInt_Array];
//Copy arrays into merged array
for(int i=0; i<lenIntArray; i++)
mergedArray=intArray[i];
for(int i=lenIntArray, j=0; i<lenIntArray+lenInt_Array; i++, j++)
mergedArray[i]=int_Array[j];

Similar Messages

  • Constructing a linked list from an array of integers

    How do I create a linked list from an array of 28 integers in a constructor? The array of integers can be of any value that we desire. However we must use that array to test and debug methods such as getFirst(), getLast(), etc...
    I also have a method int getPosition(int position) where its suppose to return an element at the specified position. However, I get an error that says cannot find symbol: variable data or method next()
    public int getPosition(int position){
         LinkedListIterator iter=new LinkedListIterator();
         Node previous=null;
         Node current=first;
         if(position==0)
         return current.data;
         while(iter.hasMore()){
         iter.next();
         if(position==1)
         return iter.data;
         iter.next();
         if(position==2)
         return iter.data;
         iter.next();
         if(position==3)
         return iter.data;
         iter.next();
         if(position==4)
         return iter.data;
         iter.next();
         if(position==5)
         return iter.data;
         iter.next();
         if(position==6)
         return iter.data;
         iter.next();
         if(position==7)
         return iter.data;
         iter.next();
         if(position==8)
         return iter.data;
         iter.next();
         if(position==9)
         return iter.data;
         iter.next();
         if(position==10)
         return iter.data;
         iter.next();
         if(position==11)
         return iter.data;
         iter.next();
         if(position==12)
         return iter.data;
         iter.next();
         if(position==13)
         return iter.data;
         iter.next();
         if(position==14)
         return iter.data;
         iter.next();
         if(position==15)
         return iter.data;
         iter.next();
         if(position==16)
         return iter.data;
         iter.next();
         if(position==17)
         return iter.data;
         iter.next();
         if(position==18)
         return iter.data;
         iter.next();
         if(position==19)
         return iter.data;
         iter.next();
         if(position==20)
         return iter.data;
         iter.next();
         if(position==21)
         return iter.data;
         iter.next();
         if(position==22)
         return iter.data;
         iter.next();
         if(position==23)
         return iter.data;
         iter.next();
         if(position==24)
         return iter.data;
         iter.next();
         if(position==25)
         return iter.data;
         iter.next();
         if(position==26)
         return iter.data;
         iter.next();
         if(position==27)
         return iter.data;
         iter.next();
         if(position==28)
         return iter.data;
         if(position>28 || position<0)
         throw new NoSuchElementException();
         }

    How do I create a linked list from an array of 28 integers
    in a constructor? In a LinkedList constructor? If you check the LinkedList class (google 'java LinkedList'), there is no constructor that accepts an integer array.
    In a constructor of your own class? Use a for loop to step through your array and use the LinkedList add() method to add the elements of your array to your LinkedList.
    I get an error that
    says cannot find symbol: variable data or method
    next()If you look at the LinkedListIterator class (google, wait for it...."java LinkedListIterator"), you will see there is no next() method. Instead, you typically do the following to get an iterator:
    LinkedList myLL = new LinkedList();
    Iterator iter = myLL.iterator();
    The Iterator class has a next() method.

  • Write an algorithm to sort an array of integers

    "Write an algorithm to sort an array of integers"
    I was asked this at an interview and required to do it in 5 mins. I don't think you can use sort them by putting them in a collection, i think you need to write an algorithm from scartch. Any idea what the best way to do this would have been?

    Depends what you mean by "best."
    BubbleSort is probably the only one I could write off the top of my head in five minutes, but as sorting routines go for practical use, it sucks. If you know how to write MergeSort or QuickSort or dman near anything off off the top of your head, that would proabably be better.
    No one sorting algorithm is "best" without any context, however.
    If you don't know any sorting algorithms, use google or your data structures and algorithms textbook.

  • Array of Integers please help?

    pleace can someone help me? i am new in java . i want to create an array integers? Here i hava my codes that creates an array of strings? can anybody convert that so can accept integer and modified my method to add Integers instead of strings?Thanks for your time
    public class Numbers implements Serializable {
    * A constant: The maximum number of Random numbers
    // static public int MAXSETS = 3;
    static public final int MAXSETS = 3;
    * The integer variable count will hold the number of states in the subset
    private int count = 0;
    * The array state will hold the States in the subset of states
    private String number[] = new String[MAXSETS];
    * Constructor method: Reserve space for the 50 Integers
    public Numbers() {
    for(int i=0;i < MAXSETS;i++) {
    number = new String();
    * addNumbers: adds a number to the collection of Numbers
    * a string with the new number is passed as a parameter
    public void addNumbers(String newNumber) {
    number[count] = newNumber;
    count++;
    // System.out.println("scdsddfs"+ state);
    * toString: Convert the object to a string
    public String toString() {
    // int num;
    String str = count + " ";
    for(int i=0;i < count;i++){
    // num = Integer.parseInt(str);
    str = number;
    // str = str+" "+number;
    // System.out.println("scdsddfs"+ str);
    return str;

    pleace can someone help me? i am new in java . i want
    to create an array integers? Here i hava my codes that
    creates an array of strings? can anybody convert that
    so can accept integer and modified my method to add
    Integers instead of strings?Thanks for your time[snip]
    You create an array of integers by saying
    int[] myArrayOfIntegers[SIZE_I_WANT];To convert a String to an int, use the Integer.parseInt method.
    Good luck
    Lee

  • How to add an array of integers to JList?

    I know it should be easy, but I can't see a simple way to add an array of integers to a JList.
    int[] per_ints = tempbean.getPercents();
    //missing wrapper
    JList list2 = new JList(per_ints);//wont work because JList will not take int[]

    what you should do is convert the int to string and then populate the list...
    int[] per_ints = tempbean.getPercents();
    Integer perI[] = new Integer[per_ints.length];
    for (int z = 0; z< per_ints.length; z++){   
    perI[z] = new Integer(per_ints[z]);
    JList list2 = new JList(Integer.toString(perI));
    i think this should work...but if not try to convert tempbean.getPercent() to string and making that array a string array...basically store string in the list some how....probably not the best or simplest way..but i think it should work

  • How to convert an array of Integers into String

    So how can an array of integers be converted into a string?
    As I do not have a javac with me now, I hope you guys could help.
    Can it be done this way:
    int [] value = new int[] {2,3,4,5,6,7}
    String strValue = "";
    for (i=0; i<value.length-1; i++)
    strValue += value;

    Instead of working with Strings, I would suggest you use a StringBuffer. It will improve memory utilization.
    Like this:
    int [] values = new int[] {2,3,4,5,6,7};
    StringBuffer sbTmp = new StringBuffer();
    final int size = values.length;
    for (i = 0; i < size; i++) {
    String intStr = Integer.toString(values);
    sbTmp.append(intStr);
    String strValues = sbTmp.toString();

  • Creating colour image map from an array of integers

    not really that relvant to swing, or in fact java even but i wasnt sure where to ask it so:
    i have an array of integers that represents information about each pixel in an image. i.e. for every pixel there is an integer value.
    at the moment i create another image from a quantised version of these integer values, which gives me a greyscale image, where black is low intensity info and white is high.
    however what i want to do is create a colour image like you see, for example, on weather forecasts. where low intensity info is blue, then purple, then red, orange, yellow and then white being the highest.
    i need some kind of algorithm that does this. does anybody have some code that does this already?
    thanks
    kevin

    sorry for not replying sooner sniper. thanks for your help but i have solved the problem:
    thanks to my mate marc for the code.
    import java.awt.Color;
    /** Class to assist in assigning color to a mathematical value.
    *  Data values must be normalized.
    *  @author Marc Ramsdale
    *  Written 20th August 2003
    public class ColorScale {
         /** Receives a normalized value (between 0.0 - 1.0)
          *  The constructor then creates a colour according
          *  to the selected colour scale.
          *  If value is outside range 0.0 - 1.0 colour is java.awt.Color.BLACK
          *  @param double value - (between 0.0 - 1.0)
         /** Maps the normalized value to a color
          *      BLUE      *
          * GREEN    *      *
          * RED           *
         public static Color generateColour(double value) {
          double red = 0.0;
          double green = 0.0;
          double blue = 0.0;
          Color color;
          int gradient = 4;
          int offset1 = 2;
          int offset2 = 4;
              if((value >= 0.0) && (value < 0.25)) {
                   red = 0.0;
                   green = value * gradient;
                   blue = 1.0;               
              else if((value >= 0.25) && (value < 0.5)) {
                   red = 0.0;
                   green = 1.0;
                   blue = -(value * gradient) + offset1;
              else if((value >= 0.5) && (value < 0.75)) {
                   red = (value * gradient) - offset1;
                   green = 1.0;
                   blue = 0.0;
              else if((value >= 0.75) && (value <= 1.0)) {
                   red = 1.0;
                   green = -(value * gradient) + offset2;
                   blue = 0.0;
              return new Color((float)(red), (float)(green), (float)(blue));

  • Java - Instance variable that can reference an array of integers?

    Hello,
    I have the following question in my assignment:
    Your first task is to declare a private instance variable called cGroups that can
    reference an array of integers. You should then write a constructor for GameN. This should
    take a suitable array as its argument which should be used to initialise cGroups.
    In order to test your code for declaring and initialising cGroups, you should execute
    the following code
    int[] coin = {2, 8, 5};
    GameN aGame;
    aGame = new GameN(coins);
    and I have complete the code below but it's not working. maybe I'm reading it wrong or
    not understandin something. Could someone correct it? or put me on the right lines
    private int[] cGroups = new int[]
    public GameN(int group1, int group2, int group3)
    super();
    this.cGroups = {group1, group2, group3};
    }

    ShockUK wrote:
    I've changed it to
    private int[] coinGroups = new int[] {1};
    public Nim(int group1, int group2, int group3)
    super();
    int[] coinGroups = {group1, group2, group3};
    }But I still get the same error "Constructor error: Can't find constructor: Nim( [I ) in class: Nim"
    Look at the line that error is pointing at. You're trying to use a constructor that doesn't exist. This is not a problem with creating an array.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Functions to read and display an array of integers

    Hi guys,
    I am trying to implement with a class called Sort a function to read in an array of integers from a file and a function to display that array.
    public class Sort
         public static int[] read(String file) throws IOException
            /* Create Reader object to read contents of file */
            BufferedReader br = new BufferedReader(new FileReader(file));
            /* Read file and store lines in ArrayList */
            ArrayList<Integer> list = new ArrayList<Integer>();
            /* Variable */
            String in;
            /* While not equal to null reads data from file */
            while ((in = br.readLine()) != null)
                /* Adds data from file to ArrayList */
                list.add(Integer.parseInt(in));
            /* Converts ArrayList into an array */
            int[] array = new int[list.size()];
            for(int i = 0; i < list.size(); i++)
                /* Get array elements */
                array[i] = list.get(i);
            /* Returns array */
            return array;
        public int display()
            for (int i = 0; i < list.size(); i++)
                System.out.println(list.get(i));
    }I am having one of those moments when you forget everything, I need help with display the array in a seperate function, will someone help me please?
    Thanks!

    I know that its just implementing it.
    I have:
    public void display()
            for(int i = 0; i < array.length; i++)
                System.out.println(array);
    But I'm lost on how to make this work, i.e it says "cannot find array variable". I've not done Java for a while and feel like I have forgot everything!
    Edited by: mbruce10 on Nov 15, 2009 1:31 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Getting Largest Int From an Array of Integers

    public class TestMe
        public TestMe(int ai[])
            _numbers = ai;
    public int getBiggest1()
            int i = _numbers[0];
            for(int j = 1; j < _numbers.length; j++)
                if(_numbers[j] >= _numbers[j - 1] && i < _numbers[j])
                    i = _numbers[j];
            return i;
        public int getBiggest2()
            double d = Math.random();
            int i = _numbers[0];
            for(int j = 1; j < _numbers.length; j++)
                if(d >= 0.10000000000000001D && i < _numbers[j])
                    i = _numbers[j];
            return i;
      private TestMe()
        private int _numbers[];
    }can anyone see what is wrong with either of the two methods as regards returning the largest integer from an array of integers.
    As far as I can see they work no matter what ints are in the array.
    Any ideas anyone ?
    Regards
    Sean

    I used the following client program to use ur class:
    I also modified ur class a bit and made the other constructor (the one with no arguments) as public to extend the class.
    Both the functions, viz, getBiggest1() and getBiggest2() are working fine...
    Now, I am new to JAVA so I might have made mistakes. Please correct me if there is anything wrong.
    /*Here is ur class that i have modified*/
    public class GetBiggest {
         public GetBiggest(int ai[])     {
              _numbers = ai;   
         public int getBiggest1()     {
              int i = _numbers[0];
              for(int j = 1; j < _numbers.length; j++)           
              if(_numbers[j] >= _numbers[j - 1] && i < _numbers[j])
                   i = _numbers[j];
              return i;
         public int getBiggest2() {       
              double d = Math.random();
              int i = _numbers[0];       
              for(int j = 1; j < _numbers.length; j++)           
              if(d >= 0.10000000000000001D && i < _numbers[j])
                   i = _numbers[j];
              return i;
    public GetBiggest()     {
    private int _numbers[];
    /*Ur class ends here*/
    /*My client program starts here*/
    import java.lang.*;
    public class TestBiggest extends GetBiggest
         public static void main (String args[])
              int[] arrNums = {30, 20, 40, 10};
              GetBiggest GB = new GetBiggest(arrNums);
              int greatest = 0;
              try
                   greatest = GB.getBiggest2();
                   System.out.println("\nBiggest number is : " + greatest);
              }//end try block
              catch(Exception e)
                   System.out.println("Some error");
              }//end catch block
         }//end main
    }//end class
    /*End of my client program*/

  • Best way of saving an array of integers to use later?

    I am having trouble figuring out where to initialize an array of integers so it is available everywhere. It's just a simple array of integers to be used as indices into another array. So I figured an ordinary C array like this is sufficient:
    int arrayOfIndices[] = {3, 11, 12, 25, 26, 43, 50, 53, 58, 59, 62, 76, 77, 81, 94, 95, 110, 113, 114, 117, 118, 123, 136, 152};
    It's ok to just use an ordinary C array for this, right? It seems that it is much harder to initialize a similar NSArray of integers. Or is there an easy way to do that?
    One problem I'm having is I'm having trouble figure out where to stick the above array so that it is usable through the program. With my NSArray objects I've been defining them in the interface, then initializing them in viewDidLoad with retain.
    Is there something analogous I can do with ordinary C int arrays? Or, alternatively, an easy way to initialize an NSArray of integers so I can deal with them like I do with other NSArrays?
    Thanks,
    doug

    Never mind. It looks like just not putting anything in the interface file and sticking the definition of the C int array at the top of my implementation file where I import files and define some other stuff works fine.
    doug

  • Passing an array of integers to a method...

    here's the case, i have 2 array of integers parent and child. how can i pass the arrays in a method so i can evaluate the contents of both arrays?
    here's the block of the code that contains what i'm talking about.
    for(int i=0; i<tree.length; i++) {//separate child from parent      
         int[] parent = new int[(tree.length()/2)];
         int[] child = new int[(tree[i].length()/2)];
         for(int j=0, ctr=0; j<tree[i].length(); j++, ctr++) {               
              parent[ctr] = Integer.parseInt(Character.toString(tree[i].charAt(j))); //get the parents
              j++;
              child[ctr] = Integer.parseInt(Character.toString(tree[i].charAt(j))); //get the children
         if(isTree(parent, child)) //evaluate if case is a tree
         System.out.println("Is a tree");
         else
         System.out.println("Is not a tree");
    }//separate child fronm parent     

    geez, thanks... adding "static" did work.. >_< my bad, i'm new in java... and i'm sorry for calling you
    "sir", thanks a lot monica... ^^ by the way, it's 1:00 pm here asia.I'm glad it worked--do you understand what "static" means here?
    No problem about the "sir". I usually use "he" myself, if I don't know whether it should be he/she. And, around here on the forum (and software engineers at most companies), there are certainly more men than women. People tend to look at MLRon and assume my name is "Ron", but that's my last name. :)
    You don't need to call people "sir" or "ma'am" here. Just names or screen names (like MLRon) will do. :)
    Have a nice afternoon, then!

  • Pass an array of integers from C to Java with JNI

    Hello,
    I have a C function that returns a struct me. The fields of this struct are all of type integer. I, with the fields of this structure create an array of integers in C language I would like to pass this array of integers c, in Java using JNI.
    How can I make this?

    I don't see how you compiled that.
    It certainly will not compile for me.
    jintArray position=(jintArray)(*env)->NewIntArray(env,2);That is the invocation idiom for C code.
    jint f[2];You cannot have a variable declaration in the middle of a method (block) in C code. That is only allowed in C++.
    C code and C++ code is not the same.
    The following is C++ code. Note that it does NOT include getBinaryPoint() since that method could be the source of the problem.
    jintArray position = env->NewIntArray(2);
    if(position==NULL)     return NULL;
    jint f[2];
    f[0]=3;
    f[1]=4;
    env->SetIntArrayRegion(position,0,2,f);
    return position;

  • Need help: merge array

    Hi everybody
    Need help in merging array of strong edges (coordinates).
    Already using array in 'if else' but have problem in merging those sequence values into two arrays of x and y coordinates (it will take all coordinates in image).
    Any ideas?
    Thanks.
    mySiti
    for (int i = 0; i < width; i++) { //for width
                for (int j = 0; j < height; j++) { //for height
                    Color c = pic.get(i, j);
                    int r = c.getRed();
                    int g = c.getGreen();
                    int b = c.getBlue();
                   int finalEdgeR=0, finalEdgeG=0, finalEdgeB=0;
                   int notEdgeR, notEdgeG, notEdgeB;
                   int [] StrongEdgeCoordX = new int ;
    int [] StrongEdgeCoordY = new int [j];
    if(r>=50 && r<=255)
    finalEdgeR=r;
    System.out.println("--------------------------------");
    System.out.println("coordinate of this pixel '"+i+"'");
    System.out.println("coordinate of this pixel '"+j+"'");
    System.out.println("StrongEdgeCoordX '"+i+"'");
    System.out.println("StrongEdgeCoordY '"+j+"'");
    System.out.println("edge red '"+finalEdgeR+"'");
    else if (r>=0 && r<50) notEdgeR=0;
    if(g>=50 && g<=255)
    finalEdgeG=g;
    System.out.println("edge green '"+finalEdgeG+"'");
    else if (g>=0 && g<50) notEdgeG=0;
    if(b>=50 && b<=255)
    finalEdgeB=b;
    System.out.println("edge blue '"+finalEdgeB+"'");
    else if (b>=0 && b<50) notEdgeB=0;

    hi, i have here a codes but i dont know on how to print the value that stored in array C;
    heres the sample code;
    // your array a and b
    int[] a, b;
    // you fill them
    a = ...;
    b = ...;
    // now you merge them
    int cSize = a.length + b.length;
    int[] c = new int[cSize];
    // pos of the c array
    int count = 0;
    int i = 0;
    for(i = 0; i < a.length; i++){
    c[count++] = a;
    for(i = 0; i < b.length; i++){
    c[count++] = b[i];

  • Merge array help..

    i made a simple program to merge arrays..it doesnt work for some reason..i post the code..can anyone help me fix it..thanks!
    public class Merging {
      public static void main (String [] args) {
        int [] list = {2, 6, 8, 4, 5, 9, 7, 6, 3, 1, 0};
        int [] list2 = {5, 6, 9, 8, 5, 1, 2, 3, 4, 9, 5, 6};
        int [] list3 = new int[list.length + list2.length];
        for (int i = 0; i < list3.length; i++) {
          list3[i] = list[i] + list2;
    System.out.print(list3[i]);
    Message was edited by:
    antonio_montana

    for (int i = 0; i < list1.length; i++) {
          list3[i] = list1;
    for (int i = 0; i < list2.length; i++) {
    list3[i + list1.length] = list2[i];
    Definately not the cleanest way, but...

Maybe you are looking for

  • IDOC not displayed in billing document

    Hi Experts, I have a issue about the outbound idoc's generated from two different billing document. I can see the idoc's from these documents though the path Goto>header>Output ---> Processing log. But if i check the services of object button for one

  • User exit or BAdi for field update in ECC system for SUS supplier

    Dear Guru's, I am configuring MM-SUS scenario in our srm 7.01 scenario. Not all vendors of our client are going to be SUS vendors. For SUS vendors, the client will be using Purchase Order Acknowledgement and Advanced Shipping Notification key. I am t

  • INCOTERMS : Different Incoterms for a Customer by Plant

    Hi there, I have a customer - ABC. ABC's sales orders are fulfilled from 2 Plants - X and Y. X and Y are in two different countries (one in EU, one non-EU). Rerquirement is to have different incoterms based on what plant the customer's sales order is

  • Enhancement in VA01 Additional data B tab

    Hi All, I have added 2 fields in tcode VA01 in header --> Additional Function --> Addtional data B (screen 8309) which are getting saved in VBAK table in custom fields which i have added in VBAK table using append structure. Now my requirement is, fo

  • Can´t Open Model XML file - URGENT

    Hi all, I have made a model in SQL Developer Data Modeling, and save it. Now when I open the xml file the model is empty, but the all the information about entities and relations are on the other directory xml files. I send the main xml and the whole