0

My Pair class:

public class Pair<o,t> {

      private o one;
      private t two;

      public Pair(o one, t two) {
        this.one = one;
        this.two = two;
      }

      public o getO() { 
          return one;
      }

      public void setO(o one) {
          this.one = one;
      }

      public t getT() {
          return two;
      }

      public void setT(t two) {
          this.two= two;
      }
}

In my main application I fill a list with the pair object:

List<Pair<Integer, String>> test = new ArrayList<Pair<Integer, String>>();
Pair<Integer, String> pair;

pair.setO(1);
pair.setT("A");
test.add(pair);

pair.setO(2);
pair.setT("B");
test.add(pair);

I want to be able to get the integer value only by list index, eg. for-loop to write out A, B, etc. How would I do that? Is there a simpler way of doing such a thing in Java?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
double_o
  • 69
  • 3
  • 10
  • when you iterate over the list you iterate over Pair, so use .getT() – Marco Acierno Jul 17 '14 at 20:41
  • "*Is there a simpler way of doing such a thing in java?*" simpler than what? Also in what meaning simpler? If you don't want to use loops you can use streams but I am not sure if that is what you want. – Pshemo Jul 17 '14 at 20:41
  • Just a quick note, but the Java Naming Conventions state that accessors and mutators (getters and setters) should be of the form `(get/set)`. For example, in your case, `getTwo` and `setTwo`. – christopher Jul 17 '14 at 20:44
  • What do you mean by integer value? – JamesB Jul 17 '14 at 20:48
  • Do you perhaps want to sort added Pairs to order them according to value of integer `o` and after they are sorted print value of `t`? Your question is kind of vague. We don't actually know what problem you are facing so it is hard to figure out how to help you... – Pshemo Jul 17 '14 at 20:59
  • Thanks guys! Good input that led me onto the right direction. also thanks for outlining the naming convention ;) – double_o Jul 17 '14 at 21:07
  • possible duplicate of [Ways to iterate over a List in java?](http://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java) – Kevin Panko Jul 17 '14 at 21:27

3 Answers3

1

for-loop to write out A, B, etc. How would I do that?

Use The For-Each Loop for Iterating over a collection.

for(Pair<Integer, String> pair : test ){
     System.out.println(pair.getT());
}
Braj
  • 46,415
  • 5
  • 60
  • 76
1

As @Braj suggested, you can use a for-each loop.

for(Pair<Integer, String> p : test ){
    System.out.println(p.getT());
}

However, because of the way you are adding objects, the output will actually be:

B
B

This is because when you add an object to the ArrayList, you are actually adding a reference to that object, so when you modify it, you will modify the one stored in the ArrayList as well. A separate copy of the object is not created! You need to construct a new Pair object for this to work as you intend.

Another note: you haven't used a constructor to initialize pair, so javac will yell at you.

To get around both of theses errors, change your code to this:

List<Pair<Integer, String>> test = new ArrayList<Pair<Integer, String>>();
test.add(new Pair<Integer, String>(1, "A"));
test.add(new Pair<Integer, String>(2, "B"));
for(Pair<Integer, String> p : test ){
    System.out.println(p.getT());
}
McLovin
  • 3,554
  • 1
  • 14
  • 15
1

If you want to get by index:

int i = ... ;
t theT = test.get(i).getT();
o theO = test.get(i).getO();

Also a bit of advice: capitalize your generic types always, makes for less confusion because names that start with a lower case letter are generally objects and not a class/type.

dammkewl
  • 622
  • 5
  • 23