Pairs
Objective: Write program code to create, traverse, and manipulate elements in 1D array or ArrayList objects.
Given a list of numbers (stored in an array), complete the method that will generate all the NumberPair
objects that are stored in an ArrayList. This problem forces you to work with both an array and an ArrayList! It is important to understand the differences in using these two data structures.
For example, if a list contained [1 2 3]
, all the possible pairs are 1&2, 1&3, and 2&3. These are all unique NumberPairs
.
The NumberPair
class is shown below.
public class NumberPair extends Pair{
public NumberPair(int a, int b){ /* implementation not shown */ }
}
public class Pair{
private int a, b;
public Pair(int a, int b){
this.a = a;
this.b = b;
}
public int getA(){ return a;}
public int getB(){ return b;}
}
for(int index1 = 0; index1 < arr.length; index1++){
for(int index2 = index1+1; index2 < arr.length; index2++){
}
}
The following are methods of a list that can be used to manipulate the contained data.