Results 1 to 2 of 2

Thread: type casting

  1. #1
    alvations is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    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.

    Java 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 09:12 PM. Reason: This is a generics problem

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •