-2

This is my code that i have now. Im trying to make variable n equal to the first element in the integer array List.I tried using the set method but that only works for String arrayLists.

ArrayList<Integer> intList = new ArrayList<Integer>();
int x = 5;// just put in after Q19.
??int n = myList;
intList.add(1);
intList.add(2);
intList.add(3);
intList.add(x);
kleopatra
  • 51,061
  • 28
  • 99
  • 211
user1261935
  • 23
  • 3
  • 5

3 Answers3

3

The first entry in an ArrayList<Integer> is accessible as an Integer via get(0). Then, to get an int from the Integer, you use intValue (although as you're using Java5 or later, you don't need to; the compiler will automatically unbox it for you):

So:

int n = intList.get(0).intValue(); // If you want to be explicit
int n = intList.get(0);            // If you want to use auto-unboxing

Note that the list could contain null values, so a bit of defensiveness might be appropriate:

int n;
Integer i = intList.get(0);
if (i != null) {
    n = i;                         // Using auto-unboxing
}
else {
    // Do whatever is appropriate with `n`
}

The question is fairly unclear. If you're looking to have some kind of enduring link between the two, you can't (not with n being int). But since Integer is immutable, I don't see any reason you'd want that, as the only real reason for an enduring link between the two would be so you could see state changes using either reference, and with immutable objects there are no state changes.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

I guess you want to have duplicate element in your collection, you can't have duplicate element in Set, like definition of set in mathematical. Oracle's Java says for Set collection:

A collection that contains no duplicate elements. 
More formally, sets contain no pair of elements e1 and e2 
such that e1.equals(e2), 
and at most one null element. As implied by its name, 
this interface models the mathematical set abstraction.

but this restriction don't exist in List collection, Oracle's Java says for 'List' collection:

Unlike sets, lists typically allow duplicate elements. 
More formally, lists typically allow pairs of elements e1 and e2 
such that e1.equals(e2), and they typically allow multiple null elements 
if they allow null elements at all. 
It is not inconceivable that someone might wish to implement 
a list that prohibits duplicates, 
by throwing runtime exceptions when the user attempts to insert them, 
but we expect this usage to be rare.

So you can't have duplicate elemet in your set instance.

for more information see following question in this site:

Blockquote

Community
  • 1
  • 1
Sam
  • 6,770
  • 7
  • 50
  • 91
  • The question is a bit vague, but I don't think you've answered what was being asked - I think the reference to `set` in the question is relating to `List.set()` rather than `java.util.Set` (even though calling `set` in order to _get_ an item is clearly wrong) – Tim Mar 11 '12 at 07:25
  • Maybe your comment is correct, but i guess he/she want to have duplicate element in collection. – Sam Mar 11 '12 at 07:27
  • probably, but since he/she is using an `ArrayList` that's not going to be an issue. – Tim Mar 11 '12 at 07:29
0

looks realy like homework:-D

int n = intList.get(0)

I dont think you need the IntValue() part, as you are already dealing with integer.

.get(0)

is the first element of the list.

.get(1)

would be the second and so on.

and of course, you should FIRST add elements to your list and then try to make n equals the first element of the list.

blackmamba
  • 141
  • 1
  • 6