Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays to Vectors

Status
Not open for further replies.

DMAN3021

Programmer
Aug 20, 2002
40
CA
Hello,

I have a Vector of int[]. Now I want to take one of these int[] and put it into a variable. Heres the code I have so far:

int[] CurNode = new int[8];

CurNode = Tree.elementAt(i);

Tree is a Vector Containing 8 arrays of integer, all with 8 elements in them. I want to take one of the elements, and put it in CurNode. I get the following error:
Code:
AITree.java:52: incompatible types
found: java.lang.Object
required: int[]
        CurNode = Tree.elementAt(i);
                                ^
Thanks for your help,

Dan...

 
Vectors, ArrayLists and related lists all hold Object types. You must cast your Objects to their appropriate type before usuage when extracting from a Vector.

An int array (int[]) is regarded by Java as an object, so you are able to cast straight from Object to int[] as below.

Code:
		int[] arr = new int[] {1,2,3,4};
		Vector v = new Vector();
		v.addElement(arr);

		int[] arr2 = (int[])v.elementAt(0);
		
		System.err.println(arr2[0]);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top