Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Alternating Numbers Of two sequences

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Alternating Numbers Of two sequences

    I am trying to alternate the numbers of two Sequences. Right now getting an infinite loop.
    any suggestions? Here is my method.

    public ArrayList<Integer> merge (Sequence other)
    {
    for (int i =0; i < other.values.size(); i++)
    {
    if (i%2==0)
    {

    for (int j =0; j < values.size(); j++)
    {
    other.values.add(i, values.get(j));


    }
    }

    }

    return other.values;
    }


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Alternating Numbers Of two sequences

    Can you please explain
    trying to alternate the numbers of two Sequences
    and what's the structure of Sequence Class.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Alternating Numbers Of two sequences

    The Following is my sequence class. I have two Sequences. I am passing the other sequence into the merge method . I am using modulo and an if statement insert at each even iteration of the elements of the first sequence. Right now I am getting an infinite loop and am not sure of where to go from here.




    public class Sequence {

    private ArrayList <Integer> values;

    public Sequence ()
    {
    values= new ArrayList <>();
    }
    /**
    *
    * @param n
    */
    public void add (int n)
    {
    values.add (n);
    }
    /**
    *
    * @return
    */
    public String toString()
    {
    return values.toString();
    }
    /**
    *
    * @param other
    * @return
    */
    public ArrayList<Integer> append (Sequence other)
    {
    for (Integer value : other.values)
    {
    this.values.add(value);
    }
    return values;
    }

    /**
    *
    * @param other
    * @return
    */
    public ArrayList<Integer> merge (Sequence other)
    {
    for (int i =0; i < other.values.size(); i++)
    {
    if (i%2==0)
    {

    for (int j =0; j < values.size(); j++)
    {
    other.values.add(i, values.get(j));


    }
    }

    }

    return other.values;
    }

    }


    In the main method I have the following:

    Sequence firstLine = new Sequence();
    Sequence secondLine = new Sequence ();

    firstLine.add(1);
    firstLine.add(4);
    firstLine.add(9);
    firstLine.add(16);

    secondLine.add(9);
    secondLine.add(7);
    secondLine.add(4);
    secondLine.add(9);
    secondLine.add(11);
    firstLine.append(secondLine);
    firstLine.toString();
    System.out.println(firstLine);
    firstLine.merge(secondLine);
    System.out.println(firstLine);

    --- Update ---

    Correction:
    The Following is my sequence class. I have two Sequences. I am passing the "other" sequence into the merge method . I am using modulo and an if statement to insert at each even iteration the elements of the first sequence. Right now I am getting an infinite loop and am not sure of where to go from here.

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Alternating Numbers Of two sequences

    So far these are my changes to the method.


     
    public String merge (Sequence other)
      { 
            for (int i =0; i < other.values.size(); i++)  
            {  // System.out.println("change");
                //System.out.println("pos" + pos + "i" + i );
     
     
                if ( i%2==0 && pos<=3)
                {   
     
                    list = list + values.get(i)+ DEL;
                    list= list + other.values.get(pos)+ DEL; 
                    //System.out.println("seqOne " + list);
                }
     
                if ( i%2==1&& pos<=3 )
                {   
                    list = list + values.get(i)+ DEL; 
                    list= list + other.values.get(pos)+ DEL;
     
                     // System.out.println("seq2 " + list);
                }
     
                if ( i%2==0 && pos==4)
                {   
     
     
                    list= list + other.values.get(pos); 
                   // System.out.println("seqOne " + list);
                }
     
     
                pos++;
     
     
     
               /* if (i%2==0)
                {    
                      other.values.add(i, values.get(pos));
     
                    pos++;
                }
                       */
     
      }return list;
     
    }}

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Alternating Numbers Of two sequences

    Use a ListIterator:

    public ArrayList<Integer> merge(Sequence other) {
      ListIterator<Integer> li = other.values.listIterator();
     
      for (Integer i : values) {
        if (li.hasNext()) {
          li.next();
          li.add(i);
        }
      }
     
      return other.values;
    }

    Given your code, if firstLine.merge(secondLine) is called on their initial values, it will return an ArrayList with values [9, 1, 7, 4, 4, 9, 9, 16, 11].
    Last edited by DuncanS; July 7th, 2014 at 11:14 AM.

Similar Threads

  1. (Java) Help with alternating negative and positive numbers.
    By fcknDan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 16th, 2013, 12:48 AM
  2. sequences help
    By david37370 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 1st, 2013, 02:35 PM
  3. What's wrong with my code? (generate sequences of numbers using a stack)
    By mescoff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2012, 09:12 AM
  4. Question to Pattern.matches and escape sequences
    By steppenwolf in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 04:29 PM
  5. Generate all possible sequences from Event Tree
    By masterblaster in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 22nd, 2011, 02:57 AM