• Java fucking up classes?
    3 replies, posted
Okay, so I have a Vector object, and i'm adding another vector to it, with 2 strings in it. Now, when i .get this object, and use getClass() on it, it says java.util.Vector. But, when i try to use .get on IT, it says it can't find the method... Code: [code] String name = plcname.getText(); String notetext = notes.getText(); String[] one = {worker9.getNm(),worker9.getHrs()}; String[] two = {worker10.getNm(),worker10.getHrs()}; String[] three = {worker11.getNm(),worker11.getHrs()}; String[] four = {worker12.getNm(),worker12.getHrs()}; String[][] list = {one,two,three,four}; Vector listfinal = new Vector(); int[] okays = new int[4]; int num = -1; for(String[] k : list){ num++; System.out.println("-----Worker-----"); if(!k[0].equals("") & !k[1].equals("")){ System.out.println(" -----Name:-----'"+k[0]+"'-----"); System.out.println(" -----Hours:--'"+k[1]+"'--------"); okays[num] = 1; } else{ System.out.println("Nothing"); } } for(int k : okays){ System.out.println(k); if(k==1){ Vector var = new Vector(); // Here is where var.add(list[k][0]); // I am adding var.add(list[k][1]); // the vector with listfinal.add(var); // the two strings to the bigger vector } } for(int i=0; i < listfinal.size(); i++){ Object obj = listfinal.get(i); System.out.println(obj.getClass()); //return: 'class java.util.Vector" //System.out.println(obj.get(1)); // this errors saying it can't find symbol method get(int) on class java.lang.Object when it clearly is a java.util.Vector //System.out.println("-----Worker-----"); //System.out.println(" -----Name:-----'"+(String)obj+"'-----"); //System.out.println(" -----Hours:--'"+(String)obj+"'--------"); } [/code] Help! Thanks in advance :)
You have to cast the Object to Vector first, else you can only use the methods of Object.
Oh... let me try that... Thank you very much sir, it worked. :downs:
You should set a type on your vectors also. [code] Vector<String> listFinal = new Vector<String>(); Vector<Integer> intVect = new Vector<Integer>(); [/code] Just good practice. And javac throws a warning if you don't set the type.
Sorry, you need to Log In to post a reply to this thread.