Question

I have an declared an ArrayList a = [1,2,3]. I created another ArrayLList b using the loop below:

for(int i = 0; i<a.size(); i++)
{
      for(int j=i+1; j<a.size();j++)
      {
              b.add("{" + a.get(i)+ "," + a.get(j) + "}");
      }
}

Now the ArrayList b will contain elements [{1,2},{1,3},{2,3}]. Now if I print the statement using System.out.println(b.get(0)), then the output will be {1,2}.

Now I want to to further explore the array such that I can extract 1 and 2 separately. How can I achieve this?

Was it helpful?

Solution

create class pair

class Pair{
String a 
String b
....
///setters and getters
} 

now let b will be List<Pair> so instead calling b.add("{" + a.get(i)+ "," + a.get(j) + "}"); you can do simple b.add(new Pair(a.get(i),a.get(j)); then you don't need to play with splitting string and stuff like that, you can easly access your values by doing ie b.get(0).getA() or b.get(0).get()

you can also override method to string in pair

public String toString() {

    return "{" + a+ "," + b + "}";
}

so when you do System.out.println(a.get(0)) you will get exactly same output like before

***EDIT if you want to have a groups of more than 2 elements as you say in comment you can construct your class little bit different

class MyClass{ 
List<Integer> fields = new ArrayList<Integer>();
//two constructors
MyClass(int singleVal)
{
fields.add(singleVal);
}


MyClass(MyClass a, MyClass b)
{
fields.addAll(a.fields);
fields.addAll(b.fields);
}
//getters setters depends what you need


}

both of your list will be list of MyClass, when you populate list a, you create objects by using first constructor, when you want to add elements to your b list you can do b.add(new MyClass(a.(i),a.(j))) but you can also do b.add(new MyClass(a.(i),b.(j))) or b.add(new MyClass(b.(i),b.(j)))

OTHER TIPS

I understand that your array b holds just strings. Use any String tokenizing mechanism to achieve this - either String.split or StringTokenizer

Hint : StringTokenizer performs better

You should decompose the presentation from logic.

In your loop you create a pair of element. So create a some type to represent it.

class Pair {

 private int left;
 private int right

 Pair(int left, int right) {
   this.left  = left;
   this.right = right;
 }

 public int getLeft() {
   return this.left;
 } 

 public int getRight() {
   return this.right;
 }

 @Override
 public void toString() {

     return String.format("{%d , %d}",getLeft(),getRight());
 }
}

List list = new ArrayList();

for(int i = 0; i<a.size(); i++)
{
      for(int j=i+1; j<a.size();j++)
      {
              list.add(new Pair(i,j));
      }
}

Then to access (extract) items

int l = list.get(0).getLeft();
int r = list.get(0).getRith();

If you want to display on console the result.

for(Pair p : list) {
  System.out.println(p);
}

About output format


EDIT

AS the OP mentioned in the comment. He would like to have flexible data structure that allow him to store the data.

List<List<Integer> parent = new ArrayList<List<Integer>()

for(int i = 0; i<a.size(); i++)
{

 List<Integer> child = new ArrayList<Integer>();

      for(int j=i+1; j<a.size();j++)
      {
              child.add(a.get(i));
              child.add(a.get(j));
      }

  parent.add(child);
}
String ele = b.get(0);
ele = ele.replace("{", "");
ele = ele.replace("}", "");
StringTokenizer tokens = new StringTokenizer(ele, ",");
while(tokens.hasMoreTokens()) {

    int val = Integer.parseInt(tokens.nextToken());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top