Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

My Preparation snippets (Collections!)

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.ArrayList;
import java.util.Iterator;

public class CollectionTest{

public static void main(String[] args) {
ArrayList list = new ArrayList();

list.add("bala");list.add("janu");
list.add("paapu");list.add("umma");

Iterator iterator = list.iterator();
while(iterator.hasNext()) {
System.out.println("Hi");
}
}
}
//This code will go on printing Hi indefinitely J Read below on what hasNext() does
public boolean hasNext()
Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)
Returns:
true if the iterator has more elements.

public class CollectionTest{

public static void main(String[] args) {
ArrayList list = new ArrayList();

list.add("bala");list.add("janu");
list.add("paapu");list.add("umma");
String []a = null;
String obj[] = (String[])list.toArray(a);
System.out.println(obj.length);
System.out.println(list);
}
}
While running throws NullPointerException. We have to give valid initialized array as argument to the toArray() method.
import java.util.ArrayList;

public class CollectionTest{

public static void main(String[] args) {
ArrayList list = new ArrayList();

list.add("bala");list.add("janu");
list.add("paapu");list.add("umma");
String []a = new String[3];
String obj[] = (String[])list.toArray(a);
for(int i=0; i< a.length; i++)
System.out.println(a[i]);
}
}
prints null, null, null..

import java.util.ArrayList;
import java.util.HashSet;

public class CollectionTest{

public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(list);
System.out.println(list); // prints [(this Collection)]

HashSet set = new HashSet();
list.add(set);
System.out.println(set); // prints []
}
}
A special case of this prohibition is that it is not permissible for a set to contain itself as an element.
 
Get out of my mind! Look! A tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic