Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-13-2008, 07:57 PM
Member
 
Join Date: Oct 2008
Posts: 19
Rep Power: 0
alvations is on a distinguished road
Default type casting
ArrayList<String> test = new ArrayList<String> ();
List<List> test2 = new ArrayList<List>();
List<List> test3 = new ArrayList<List>();


test.add("pig");
test2.add(test);
test3.add(test2);

//System.out.println( ((List) ((List)(test3.get(0).get(0))).get(0)).set(0,"cow") );


__________________________________________________ ___

i've seem to asked a question on this in another thread. but here i'm asking for a different approach to solve the problem. given this construction, how do i change the value from "pig" to "cow"?? i'm sure there's some way to cast the List to accept and replace my string
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-13-2008, 08:07 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
//System.out.println( ((List) ((List)(test3.get(0).get(0))).get(0)).set(0,"cow") );
Break this down into single statements like the following:
Type1 var1 = var0.method();
Type2 var2 = var1.method();
Type3 var3 = var2.method();
etc
Then you can see what method of what class is being used.


Here's working code, but don't have good generics usage. Not sure if your approach is correct.

Code:
//how do i change the value from "pig" to "cow"

// Involves type checking and generics

import java.util.*;

public class TestIndexingLists {

	public static void main(String[] args) {
		ArrayList<String> test = new ArrayList<String>();
		List<List> test2 = new ArrayList<List>();
		List<List> test3 = new ArrayList<List>();
		
		test.add("pig");
		test2.add(test);
		test3.add(test2);
      //------------------------------------------------------------------------
      // get reference to test starting at test3
      List list_test2 = test3.get(0);
      System.out.println((test2 == list_test2) + "  list_test2:" 
                + list_test2.getClass());  // true list_test2:class java.util.ArrayList

      //-----------------------------------------------------------------------
//      ArrayList<String> list_testX = (ArrayList<String>)((ArrayList<List>)test2).get(0);
      ArrayList<String> list_testX = (ArrayList<String>)test2.get(0);
      //TestIndexingLists.java:24: warning: [unchecked] unchecked cast
      //found   : java.util.List                       <<< NB a List for test2 (a List<List>)
      //required: java.util.ArrayList<java.lang.String>
      //      ArrayList<String> list_testX = (ArrayList<String>)test2.get(0);
      //                                                                 ^

      ArrayList<String> list_test = (ArrayList<String>)list_test2.get(0);
      //TestIndexingLists.java:26: warning: [unchecked] unchecked cast
      //found   : java.lang.Object                      <<<< NB Object for list_test2 (a List<Object>)
      //required: java.util.ArrayList<java.lang.String>
      //      ArrayList<String> list_test = (ArrayList<String>)list_test2.get(0);
      //                                                                     ^
      //TestIndexingLists.java:21: inconvertible types
      //found   : java.util.List<java.util.List>
      //required: java.util.ArrayList<java.lang.String>
      //      ArrayList<String> list_test = ((ArrayList<String>)test2).get(0);
      //                                                        ^

// Changed source by removing ()before .get()
//TestIndexingLists.java:23: warning: [unchecked] unchecked cast
//found   : java.lang.Object
//required: java.util.ArrayList<java.lang.String>
//      ArrayList<String> list_test = (ArrayList<String>)list_test2.get(0);
//                                                                     ^

      System.out.println(test == list_test);     // true

      System.out.println(list_test);   // [pig]
      list_test.set(0, "cow");  // change pig to cow
      System.out.println(list_test);   // [cow]
	}
}

Last edited by Norm; 10-13-2008 at 10:12 PM. Reason: This is a generics problem
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cast string type to int type GilaMonster New To Java 9 09-17-2008 11:43 AM
type casting array? willemjav Advanced Java 5 08-27-2008 11:25 PM
Type Casting Help rhm54 New To Java 2 02-07-2008 01:06 PM
'Casting' couch !! ajaygargnsit New To Java 1 12-22-2007 02:05 PM
Casting leebee New To Java 5 08-10-2007 01:24 PM


All times are GMT +2. The time now is 12:47 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org