The Artima Developer Community
Sponsored Link

Java Answers Forum
ArrayList's

2 replies on 1 page. Most recent reply: May 5, 2002 5:38 AM by Evgen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
HB

Posts: 1
Nickname: rowdy00
Registered: Apr, 2002

ArrayList's Posted: May 5, 2002 4:27 AM
Reply to this message Reply
Advertisement
Hi all,

I'm having troubles with an ArrayList and how to add objects to an Array and then print the value of objects in the Array. I have studied the other ArrayList topics in this forum and have consulted the java API but still l'm unable to get the program to work correctly.
Here is the successfully compiled code.

Any help would be greatly appreciated.
-----------------------------------------
import java.util.*;

class Array
{
/***********************************************
** Populate an ArrayList with 20 random ints
************************************************/
public static void main(String[] args)
{
ArrayList ran = new ArrayList(20);
ListIterator i = ran.listIterator();
while(i.hasNext())
{
Integer r = new Integer ( (int) (1 +
Math.random() * 100);
ran.add(r);
}
print(ran);
}//method main

/***************************************************
** Print the int value of each object in the array
***************************************************/
public static void print(ArrayList myList)
{
ListIterator t = myList.listIterator();
while(t.hasNext())
{
Integer randomVal = (Integer)t.next();
int value = randomVal.intValue();
System.out.println(value);
}
}//method print

}//class Array


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: ArrayList's Posted: May 5, 2002 5:36 AM
Reply to this message Reply

/* ArrayListDemo.java
*/

import java.util.*;

public class ArrayListDemo{


public static void main(String[] args){
ArrayListDemo demo = new ArrayListDemo();
ArrayList myarraylist = demo.load(20);
demo.print(myarraylist);
}

/** Loads arrayList with n random int values.
*/
public ArrayList load(int n){
ArrayList arraylist = new ArrayList();
Random random = new Random();
for (int i = 0; i < n; i++){
int nextRandomInt = random.nextInt();
arraylist.add(new Integer(nextRandomInt));
}
return arraylist;
}

/** Prints an arrayList to standard output.
*/
public void print(ArrayList arraylist){
ListIterator iterator = arraylist.listIterator();
while (iterator.hasNext()){
Integer nextInteger = (Integer) iterator.next();
System.out.println(nextInteger.toString());
}
}
}

Evgen

Posts: 14
Nickname: evgen79
Registered: May, 2002

Re: ArrayList's Posted: May 5, 2002 5:38 AM
Reply to this message Reply
You hasn't troubles with add or print value of objects in the Array, there are only one bug:
while(i.hasNext())
you try to call hasNext in the step of programm where i has no value, and this cycle stoped without any steps.
If you replace this line to:
for(int j=0; j<20; j++) {
than code will start working.

Flat View: This topic has 2 replies on 1 page
Topic: Hashtable vs Vector  Collection/List vs Map Previous Topic   Next Topic Topic: adding lines in a file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use