• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

unsafe operation??

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 57
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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())...
 
reply
    Bookmark Topic Watch Topic
  • New Topic