Are you trying to implement an ArrayList or use the existing library code?
If it's the latter, the "codes" would be
ArrayList<Type> list = new ArrayList<Type>();
list.add(item);
boolean b = list.isEmpty();
list.clear();
b = list.isEmpty();
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Skill Endorsements: 3
Best way would be to try to write them. The code (mass noun, there is no "codes") is not publicly available as far as I know, but it's not too hard to work out how you'd implement these. You'll learn a lot more by writing than be reading, even if your code doesn't look like the stuff Sun is using.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Skill Endorsements: 3
You can download the source code for Java SE from the Oracle web site under the JRL (Java Research Licence*) - although there are restrictions if you live in country that's not on the approved list.
http://download.java.net/jdk6/source/
* The JRL is a license that was created specifically for universities and researchers who want to use Java technologies as subject matter for learning and research. This license is designed for the research community. This includes schools and universities as well as companies and individuals, who want to examine the technology source code for personal interest or research & development purposes. This license allows a licensee to work alone or with other licensees.
JamesCherrill
... trying to help
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
Question Answered as of 2 Years Ago by
jon.kiparsky
and
JamesCherrill Beware: the link you found is to source for the Apache "harmony" project, which is an attempt to build a fully open-sourced alternative to Sun/Oracle's original Java; it's completely new code.
There has been stupid and destructive political infighting about this, and it seems unlikely that this version will be able to get certified (unfortunately).
Its very likely that the code you find there will execute to give the same results as Sun/Oracle Java, but its not the same code. In particular it has less in the way of comments compared to Oracle version.
Here, for example are the two versions of the same method in ArrayList - first Apache:
/**
615 * Returns an array containing all elements contained in this
616 * {@code ArrayList}. If the specified array is large enough to hold the
617 * elements, the specified array is used, otherwise an array of the same
618 * type is created. If the specified array is used and is larger than this
619 * {@code ArrayList}, the array element following the collection elements
620 * is set to null.
621 *
622 * @param contents
623 * the array.
624 * @return an array of the elements from this {@code ArrayList}.
625 * @throws ArrayStoreException
626 * when the type of an element in this {@code ArrayList} cannot
627 * be stored in the type of the specified array.
628 */
629 @Override
630 @SuppressWarnings("unchecked")
631 public <T> T[] toArray(T[] contents) {
632 int size = lastIndex - firstIndex;
633 if (size > contents.length) {
634 Class<?> ct = contents.getClass().getComponentType();
635 contents = (T[]) Array.newInstance(ct, size);
636 }
637 System.arraycopy(array, firstIndex, contents, 0, size);
638 if (size < contents.length) {
639 contents[size] = null;
640 }
641 return contents;
642 }
Now Oracle:
/**
* Returns an array containing all of the elements in this list in proper
* sequence (from first to last element); the runtime type of the returned
* array is that of the specified array. If the list fits in the
* specified array, it is returned therein. Otherwise, a new array is
* allocated with the runtime type of the specified array and the size of
* this list.
*
* <p>If the list fits in the specified array with room to spare
* (i.e., the array has more elements than the list), the element in
* the array immediately following the end of the collection is set to
* <tt>null</tt>. (This is useful in determining the length of the
* list <i>only</i> if the caller knows that the list does not contain
* any null elements.)
*
* @param a the array into which the elements of the list are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of the list
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this list
* @throws NullPointerException if the specified array is null
*/
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
I still recommend downloading Oracle's official source code bundle.
JamesCherrill
... trying to help
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
@ JamesCherrill
really that's excelent point (vote snoorOracle API too), but in other hands I didn't expect that that OP would be (could be is maybe better) playing with Generics ...,
<FW>Apache API overRode basic Java API, because lots of Methods are protected in Java API</FW>
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12