Alternating Numbers Of two sequences

• July 6th, 2014, 09:55 PM
kkruz
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++)
{

}
}

}

return other.values;
}
• July 6th, 2014, 10:13 PM
aprabhat
Re: Alternating Numbers Of two sequences
Quote:

trying to alternate the numbers of two Sequences
and what's the structure of Sequence Class.
• July 6th, 2014, 10:27 PM
kkruz
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
*/
{
}
/**
*
* @return
*/
public String toString()
{
return values.toString();
}
/**
*
* @param other
* @return
*/
public ArrayList<Integer> append (Sequence other)
{
for (Integer value : other.values)
{
}
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++)
{

}
}

}

return other.values;
}

}

In the main method I have the following:

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

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.
• July 7th, 2014, 01:28 AM
kkruz
Re: Alternating Numbers Of two sequences
So far these are my changes to the method.

Code Java:

```  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;   }}```
• July 7th, 2014, 10:54 AM
DuncanS
Re: Alternating Numbers Of two sequences
Use a ListIterator:

Code java:

```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].