1

I am trying to create an ArrayList which will have 3 columns of type (Date, Date, String)

So it is really like table of multiple values, for example:

Jan 11, Jan 11, "started"
Jan 11, Jan 15, "running"
... 
Jan 20, Jan 23, "slowing" 
Jan 23, Jan 23, "stopped"

How would I go about creating an ArrayList like this? Or is there another data format that is better? Sorry I am very new to Java and trying to learn. Also, I need to be able to pass this data structure between different classes.

Thanks a lot!

2

8 Answers 8

3

Create a holder class:

 class Holder{
         //your fields : Date, Date, String
 }

Then:

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

Comments

2

In ArrayList you can't have multiple columns, ArrayList is similar to one dimensional array and you can store objects of any datatype.

The solution depends on the problem you are trying to resolve. If the data you are trying to store in one row are related and have some unique identity you can use HashMap, or you can create a class with the columns as fields and store the instances of that class in the ArrayList.

Hope this helps.

Comments

1

Create a class the have your properties then declare a list with your class type like:

public class MyClass {

    private Date d1;
    private Date d2;
    private String s;

}

Then declare list as:

ArrayList<MyClass> list = new ArrayList<MyClass>();

Comments

1
public class MyObject() {
   Date date1;
   Date date2;
   String status;

   public MyObject() {

   }

   //Getters & Setters
}

and then in main, or where you are referencing this data

List< MyObject> list = new ArrayList<>();

Comments

1

Java does not have a data structure to hold different types. However, I create my own Tuple data structure that would be appropriate for you.

Sample usage :

public class TupleTest {

@Test
public void testOneType() {
    Tuple t1 = Tuple.Factory.create(1);
    Tuple t2 = Tuple.Factory.create(1);
    assertEquals(t1, t2);
}

@Test
public void testTwoTypes() {
    Tuple t1 = Tuple.Factory.create(1, "a");
    Tuple t2 = Tuple.Factory.create(1, "a");
    assertTrue(t1.equals(t2));
}

@Test
public void testGetIndex() {
    Tuple t1 = Tuple.Factory.create(1, "a");
    assertTrue((Integer) t1.get(0) == 1);
}
}

public class Tuple {

private final Object[] items;

private Tuple(Object... items) {
    this.items = items;
}

public static class Factory {
    public static Tuple create(final Object... items) {
        return new Tuple(items);
    }
}

/**
 * 
 * @return
 */
public synchronized int size() {
    return items.length;
}

@Override
public int hashCode() {
    // TODO Auto-generated method stub
    int hash = 17;
    for (Object obj : items) {
        if (obj != null)
            hash = hash * 31 + obj.hashCode();
    }
    return hash;
}

@Override
public boolean equals(Object obj) {
    // TODO Auto-generated method stub
    boolean res = true;
    final int size = this.size();
    final Tuple p = (Tuple) obj;

    if (!(obj instanceof Tuple))
        res = false;

    if (obj == null || this == obj)
        res = false;

    if (p.size() != size)
        res = false;

    for (int i = 0; i < size; i++) {
        if (!p.items[i].getClass().equals(this.items[i].getClass()))
            res = false;

        if (!p.items[i].equals(this.items[i]))
            res = false;
    }

    return res;
}

/**
 * Return string of multiple types "String.class Number.Class Integer.Class"
 */
@Override
public String toString() {
    // TODO Auto-generated method stub
    StringBuilder sb = new StringBuilder();
    for (Object item : items) {
        sb.append(item.toString() + " ");
    }
    return sb.toString();
}

public Object get(int index) {
    return items[index];
}

 }

Comments

0
public class MyObject {
    Date d1;
    Date d2;
    String s;
}


List<MyObject> list = new ArrayList<MyObject>();

1 Comment

Please add comments or an explanation.
0

As others have already explained, the idiomatic way of doing this in Java is to create a class for your triple.

I would like to add something else though, and that is to model your actual data. Instead of calling them d1, d2 and s - name the fields for what they actually are.

I don't know what your dates represent, but this is an example of what I mean:

class Record {
    Date begin, end;
    String status;
}

List<Record> records;

Comments

0

You can use HashMap is the best method

How to use HashMap, (this example is to @Stephen C)

The value in the HashMap, to find values are sought by means of the associated key.
The Hashmap provide quick search

Examples of declaration

     //1
     HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
     //2    
     HashMap<String, ArrayList<String>> hashMap2= new HashMap<String, ArrayList<String>>()

put

//1
    hashMap.put("Value1", new Integer(1));
//2     
    ArrayList<String> arrList = new ArrayList<String>();

        arrList .add("value");

    hashMap2.put("value1", arrList );

get

//1
    hashMap.containsValue(1));
//2

while(hashMap2.hasNext()) {

      System.out.println("Key = '" + key + "' has values: " + values);

}

I hope these examples can provide a solution to many people who did not understand as "Stephen C".

2 Comments

I think this is a really bad answer. No justification, no explanation. Just an opinion. Technically wrong too ... there is no requirement for lookup, and no obvious "key" in the OP's table.
The Question is not asking for anything that involves lookup. A more appropriate solution is an ArrayList of a custom tuple class. Simpler, more efficient, and meets the stated requirement for a >>list<< data structure. In that light, your "tutorial" on using HashMap is completely irrelevant.

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.