2
Robentry [] Rb = new Robentry[robNum];

How can I store Rb into an ArrayList<E> Rt, so that Rt.get(0) == Rb is true. I do not know how to define E in the ArrayList<E>.

Edit:

If I use :

Robentry [] Rb = new Robentry[robNum];
List<Robentry []> Rt = new  ArrayList<Robentry []>();
// initialize Rb
//...
// 
Rt.add(Rb);

If I change Rb[0], Rt.get(0)[0] is also changed. So how can I store the content of Rb into Rt so that Rt is independent of Rb?

3
  • 1
    There are plenty of examples of this available. I mean, you're not showing any research effort. Commented Oct 29, 2012 at 17:56
  • Well, E is a type. I'd assume then that you want an ArrayList<RobEntry[]>, since Rb is of type RobEntry[]. Commented Oct 29, 2012 at 17:58
  • Rt.add(Rb.clone()); for your second problem. Commented Oct 29, 2012 at 18:38

5 Answers 5

7

ArrayList<Robentry[]> arrList sounds like what you want.

This means arrList is a List (or ArrayList) of Robentry[].

Sign up to request clarification or add additional context in comments.

Comments

2

Based on your modified question, you probably want

Rt.add(Rb.clone());

or

Rt.add(Arrays.copyOf(Rb, Rb.length));

Array references are passed around by value, so you have to do an explicit copy if you want the arrays to be independent of one another.

Comments

2

you can use Type Robentry[] to a List. like -

List<Robentry []> list = new ArrayList<Robentry []>();

Comments

2

You can declare and use the List of type Robentry[] as below:

    //Declare a list of type Robentry[] 
    List<Robentry[]> list = new ArrayList<Robentry []>();
    //add the rb to the list
    list.add(Rb);

    //compare the list element with Rb
    System.out.println(list.get(0)==Rb);//should print true

Please note: == is fine in above example since list element is same as Rb, otherwise equals method is recommended.

Comments

2
 ArrayList<Robentry> Rt=(ArrayList<Robentry>) Arrays.asList(Rb);

or in your ways,

ArrayList<Robentry[]> Rt=new ArrayList<Robentry[]>();
rt.add(Rb);

This is called Generics,was made to ensure that the variable should contain only specific types.See this for official docs.

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.