GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes unsafe operation?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "unsafe operation??" Watch "unsafe operation??" New topic
Author

unsafe operation??

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
import java.util.*;

class ArrayListDemo {
public static void main(String args[]){
ArrayList al=new ArrayList();
System.out.println("initial size ofal " +al.size());
al.add("C");
al.add("a");
al.add("g");
al.add("h");
System.out.println("size of al : " +al.size());
}
}


/****8what is wrong in this program**********/
am getting a error like unsafe operations??
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

Try specifying the type of data to be stored in your collection between < > where you are declaring and constructing your ArrayList.
[ November 29, 2007: Message edited by: Brandt Charles ]

Green, green, green!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This is not exactly an "error" -- it's just a warning. Your code will still compile and run.

As Brandt explained above, the warning is because you have not specified a generic type for your List. The danger is that there are no checks on what the list contains, so you could add any type of object to your list. For example, you could mix Strings with ArrayLists!

This is a problem, because if you are accessing items from this List and expecting it to contain Strings, then you will have a serious problem if you try to treat an ArrayList as a String!

With generics, if you specify a type for your list, like String...

...then attempting to add anything other than a String will result in a compilation error.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
with reference to my above program if i add this line will it show the contents of arraylist.


1> i know we have to iterate through the collection. but in one book it is give n like the below line


System.out.println("Contents of al : " +al);
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by prasanna sheregar:
...in one book it is give n like the below line

System.out.println("Contents of al : " +al);

This will give you a simple String representation of the List. Try it! It will not allow you to access specific elements of the List.
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
its given in

java2 : the complete reference by Herb Schildt Fifth edition

page 450
its given as that line shows the contents of arraylist

reason given is

1>its because of default conversion provided by toString()

/*** i am also puzzeled by this one*******/
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
but i got the output as

[C,a,g,h]

/*how is it possible without iteratin through collection(i mean arrayList)***/
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

When using println() in this case you are invoking the toString() method of an ArrayList. toString() tells an ArrayList to display its contents in the String output you have seen. If we were to check out the source code for the method, I expect there is an Iterator in there somewhere or maybe an enhanced for loop.

Like Marc said, this is only allowing us to view the contents, you are unable to manipulate them at this point. If you wanted to change the output when invoking toString() you could create, for example, MyArrayList, extending ArrayList, and override the toString() method. But that's only if you want to be extra fancy
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Also note that the String representing the List is assembled by called toString() on each of the List's elements. Since you have Strings as elements, these display nicely as "[C,a,g,h]" (because calling toString() on a String returns the represented String). Note what happens using simple Objects (i.e., with Object's implementation of toString())...
 
GeeCON Prague 2014
 
subject: unsafe operation??