0

I want to pass more items with following data types:

OneListItem = (Integer, String, Intent);

So:

OneListItem.add(1,"Hello", IntentToPass);
OneListItem.add(2,"Hello", IntentToPass);
OneListItem.add(3,"Hello", IntentToPass);

And call method which is processing given data by this way:

nh.addNotification(OneListItem);

What data structure is best to use for this (data structure will be always same) and how howt to define it?

I tried something like this but without luck:

List<String, Integer, Intent> li = new ArrayList<String, Integer, Intent>;

Thanks for any advice.

7 Answers 7

2

I would wrap this and create my own class:

class MyClass {
    private String str;
    private Integer i1;
    private Integer i2;

    // ctrs, getters, setters
}

And then simply:

List<MyCLass> li = new ArrayList<>();
Sign up to request clarification or add additional context in comments.

Comments

1

Actually... you can do also this way, that's a lot more similar to what you asked.

WARNING: I'm not suggesting to use it, I'm only saying it's possible to do so.

Intent intent;

ArrayList<Object[]> objects = new ArrayList<Object[]>();
objects.add(new Object[]{1,"Hello", intent});
objects.add(new Object[]{2,"Hello", intent});
objects.add(new Object[]{3,"World", intent});

Log.d("OUTPUT", objects.get(2)[1]); // it prints out "World"

Comments

0

You can use MultiKeyMap of apache commons or you can define your own custom class that will hold the data you require.

Comments

0

Make a class:

public class MyNotificationItem {
    private final int first;
    private final String second;
    private final int third;

    public MyNotificationItem(int first, String second, int third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public int getFirst() {
        return first;
    }

    public String getSecond() {
        return second;
    }

    public int getThird() {
        return third;
    }
}

Comments

0
public class MyNotificationItem {
    private final int first;
    private final String second;
    private final int third;

    public MyNotificationItem(int first, String second, int third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public int getFirst() {
        return first;
    }

    public String getSecond() {
        return second;
    }

    public int getThird() {
        return third;
    }
}

class OneListItem extends ArrayList<MyNotificationItem>{
     public void add(Integer i, String j, Intent k)){
         add(new MyNotificationItem(i, j, k));
    }
}

Comments

0

The simplest way is to create a class for whatever type of information you're trying to store. For example:

class Person {
    public final String name;
    public final int age;
    public final boolean gender;
    public Person(String name, int age, boolean gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }
}

And then the list would be used like so:

ArrayList<Person> people = new ArrayList<>();
people.add(person1);
people.add(person2);
people.add(person3);

The use of public final member variables for the "container" class could also be replaced with member variables declared public and not final, allowing them to be modified. It all depends on whether or not you want the class to be immutable or not and how you want to manipulate the data.

For a simple "container" class like this, I'd argue against using getters / setters because it's really just unnecessary coding. Just use public non-final variables. However, using getters or setters may be necessary to implement synchronization or other processing when the class is accessed.

Comments

0

There's no adequate representation for that in the standard library. Easiest would be to create your own Triple<A,B,C> class and then work with a List<Triple<String,Integer,Intent>>.

public final class Triple<A,B,C>{
   public final A _1; //public final to avoid getters on example
   public final B _2;
   public final C _3;

   public Triple(A a, B b, C c){
     this._1 = a;
     this._2 = b;
     this._3 = c;
  }
}

//usage would be like this
List<Triple<String, Integer, Intent>> li = new ArrayList<>;
Triple<String, Integer, Intent> t = new Triple(1,"Hello", IntentToPass);
li.add(t);

Or, you can use an external library, for example the Triple from Apache commons

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.