Get class arraylist java

Class ArrayList

Type Parameters: E — the type of elements in this list All Implemented Interfaces: Serializable , Cloneable , Iterable , Collection , List , RandomAccess Direct Known Subclasses: AttributeList , RoleList , RoleUnresolvedList

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null . In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector , except that it is unsynchronized.)

The size , isEmpty , get , set , iterator , and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be «wrapped» using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

List list = Collections.synchronizedList(new ArrayList(. ));

The iterators returned by this class’s iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException . Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Читайте также:  Equals hashcode java собеседование

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class is a member of the Java Collections Framework.

Источник

how to use java reflection to get element type of List ?

send pies

posted 15 years ago

  • Report post to moderator
  • how to get element type for a list ?

    for example
    java.util.List peopleList;

    how to use java refection to get a List element type: data.People ?

    Sheriff

    send pies

    posted 15 years ago

  • Report post to moderator
  • I don’t think this can be done, because generic types apply only at compile time. At runtime, a List is simply a List.

    For instance, Box is translated [at compile time] to type Box, which is called the raw type — a raw type is a generic class or interface name without any type arguments. This means that you can’t find out what type of Object a generic class is using at runtime.

    «We’re kind of on the level of crossword puzzle writers. And no one ever goes to them and gives them an award.» ~Joe Strummer
    sscce.org

    Wanderer

    send pies

    posted 15 years ago

    • 1
  • Report post to moderator
  • Ermmm. actually this can be done. The class file contains info about the generic types of its member variables — that info is necessary if/when you try to compile another class using classes in a jar file, for example. That info is also accessible via reflection. But only generics-related info is only available for the class in general — not for specific instances of the class. To make a concrete example:

    As you see, you can get info about the declared type of instance variable fooList. You can learn that it’s a List. However, if you try to examine an actual instance that is referenced by that variable, the most you can learn is that it’s (in this case) an ArrayList. You can’t find out whether it’s an ArrayList or an ArrayList or an ArrayList. That specific info doesn’t exist for runtime instances of a generic class. It exists for the declared type of a field, or of a method parameter or return value — but not for actual instances of the objects that are represented or referenced by those fields, parameters or return values.

    «I’m not back.» — Bill Harding, Twister

    Sheriff

    send pies

    posted 15 years ago

  • Report post to moderator
  • «We’re kind of on the level of crossword puzzle writers. And no one ever goes to them and gives them an award.» ~Joe Strummer
    sscce.org

    send pies

    posted 13 years ago

  • Report post to moderator
  • well..may be i am digging something very old here. but i want an answer for something similar available for java 1.4..or does anything similar exist.
    as for this..what’s the reason for null ownerType when the program is run?

    type: java.util.List
    raw type: interface java.util.List
    owner type: null
    actual type args:
    ? extends GenericTypeInfo$Foo

    obj: []
    obj class: class java.util.ArrayList

    Java Cowboy

    Scala

    send pies

    posted 13 years ago

  • Report post to moderator
  • shwetank singh wrote: but i want an answer for something similar available for java 1.4..or does anything similar exist.

    No, because generics did not exist at all in Java 1.4.

    shwetank singh wrote: as for this..what’s the reason for null ownerType when the program is run?

    Because the class that’s being examined doesn’t have an owner type. Lookup the API documentation of java.lang.reflect.ParameterizedType.getOwnerType().

    send pies

    posted 13 years ago

  • Report post to moderator
  • send pies

    posted 13 years ago

  • Report post to moderator
  • Thanks Jesper..and Jim..for wonderful insight!
    perhaps i could seek your comments on something similar..

    ..using reflection (java.lang.reflect) in JDK1.4, am trying to create a generic function to create a hibernate Criteria ..the task is so organized that i’ve to write a very generic function that anyone can call and use by passing a reference of class whose criteria has to be created.
    now the question, i’ve an object of type A that has a List..the List contains objects of type B, C..etc. i wish to find out their type (of B,C,etc..) (similar to OwnerType here) at runtime.

    do we have any way to make that possible in java 1.4. to add to this, i can’t use instanceOf test as the component in which this utility goes has no other knowledge than the Object being passed to it.

    ..again, please let me know if this should be posted as a new topic..am posting here for some inquisitive minds who may be looking out for this solution in java 1.4

    Sheriff

    Chrome

    send pies

    posted 13 years ago

  • Report post to moderator
  • In Java 1.4 all you can do is make a guess. You loop through all the elements and find out a common super class (worst case this will be java.lang.Object) or a number of common interfaces (0 or more). The common super class for any two classes X and Y is easy:
    — if X is a direct or indirect super class of Y then it’s X
    — if Y is a direct or indirect super class of X then it’s Y
    — otherwise it’s the common super class of X.getSuperclass() and Y.getSuperclass()

    For interfaces it’s also not that hard. You need to collect all interfaces a class implements using Class.getInterfaces() (don’t forget the super classes!). Collect these in a Set. Do this for all elements, each time using Collection.retainAll. In pseudo code:
    Of course you can get multiple results, so you would have to take a pick.

    SCJP 1.4 — SCJP 6 — SCWCD 5 — OCEEJBD 6 — OCEJPAD 6
    How To Ask Questions How To Answer Questions

    send pies

    posted 11 years ago

  • Report post to moderator
  • The solutions provided are good. I could also suggest that instead trying to get it from the List itself, check the List for null and obtain the first element if that’s not the case.

    Then depending on the instance returned you can use

    if (List.get(0) instanceof MyCustomClass)

    Marshal

    send pies

    posted 11 years ago

  • Report post to moderator
  • Marshal

    send pies

    posted 11 years ago

  • Report post to moderator
  • send pies

    posted 10 years ago

  • Report post to moderator
  • Maybe there are some magic with «Field», I cannot get it working for a local variable (or parameter). for this code:

    see the «class» it has already erased info about String.

    Does anyone know another solution or I did something wrong?

    Jim Yingst wrote: Ermmm. actually this can be done. The class file contains info about the generic types of its member variables — that info is necessary if/when you try to compile another class using classes in a jar file, for example. That info is also accessible via reflection. But only generics-related info is only available for the class in general — not for specific instances of the class. To make a concrete example:

    As you see, you can get info about the declared type of instance variable fooList. You can learn that it’s a List. However, if you try to examine an actual instance that is referenced by that variable, the most you can learn is that it’s (in this case) an ArrayList. You can’t find out whether it’s an ArrayList or an ArrayList or an ArrayList. That specific info doesn’t exist for runtime instances of a generic class. It exists for the declared type of a field, or of a method parameter or return value — but not for actual instances of the objects that are represented or referenced by those fields, parameters or return values.

    Источник

    Оцените статью