The Artima Developer Community
Sponsored Link

Java Answers Forum
help with a if statement

3 replies on 1 page. Most recent reply: Dec 20, 2002 2:57 PM by Matt Gerrans

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 3 replies on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

help with a if statement Posted: Dec 19, 2002 11:21 AM
Reply to this message Reply
Advertisement
hello everyone i hope someone can help me
if you look at my code below the arraylist contains the number 8. but i want to know how i can use that number 8 inside the if statement (if (i%10 == 1)) where the number 1 is, so that number 1 will change to the number 8.
could someone please help me.
thanks for your time
ben
hash.put(new Integer(8), st1);
if(hash.containsValue(st1)) {

Enumeration e = hash.keys();
while (e.hasMoreElements()) {
Object it = e.nextElemenet();
Object it1 = hash.get(it);
if (it1.equals(st1){
arraylist.add(it);
}
}
Enumeration e = v.elements();
Object o;
for(int i = 1;e.hasMoreElements();i++) {
o = e.nextElement();
if (i%10 == 1){
System.out.println(o);
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help with a if statement Posted: Dec 19, 2002 5:31 PM
Reply to this message Reply
It isn't very clear what you are trying to do, but you might replace the 1 with
((Integer)arraylist.get(arraylist.size()-1)).intValue()
if you want to use the last value added to the ArrayList (if that is the case, you may want to look at using a LinkedList, by the way).

By the way, why are you using the old contructs like Hashtables and Enumerations instead of using the easier-to-use new APIs, such as Iterator and Map? Also, why hard-code the datatype into the name (arraylist)? In any case, why not just use a List, which might be an ArrayList, or some other type of List, like a LinkedList? I generally prefer to create my lists and maps and other things like this:
List kingPawnOpenings = new ArrayList();
Map commonPositions = new HashMap();
Then at some later time, if I decide I'd rather be using a LinkedList or TreeMap, making the change is simpler.

Also, it looks like you are just experimenting now, but when you start writing some real production code, it is a good idea to use more descriptive variable names than "it" and "it2."

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: help with a if statement Posted: Dec 20, 2002 5:23 AM
Reply to this message Reply
hello matt thanks for the idea
and i know my code is not the best but when i have completed what i want to be able to do i will look into how i can better it.
just another quick question
arr2 contains 4 integers in it i get the first value from the arraylist ok but how would i then get the next value out of the arraylist?
thanks for your time
ben
hh.put(new Integer(1), st1);
hh.put(new Integer(2), st2);
hh.put(new Integer(3), st3);
hh.put(new Integer(4), st4);
hh.put(new Integer(5), st5);
hh.put(new Integer(6), st6);
hh.put(new Integer(7), st7);
hh.put(new Integer(8), st8);
hh.put(new Integer(9), st9);
hh.put(new Integer(10), st10);
if (hh.containsValue(st1)){
Enumeration e11 = hh.keys();
while (e11.hasMoreElements()) {
Object IT = e11.nextElement();
Object IT1 = hh.get(IT);
if (IT1.equals(st1)){
arr2.add(IT);



}
}
}

Enumeration e = v.elements();
Object o;
for(int i = 1;e.hasMoreElements();i++) {
o = e.nextElement();
if (i%10 == ((Integer)arr2.get(arr2.size()-1)).intValue()){

System.out.println(o);
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help with a if statement Posted: Dec 20, 2002 2:57 PM
Reply to this message Reply
Here is a simple sample of filling an ArrayList, then getting all the items from it. I'm taking advantage of the fact that Object.toString() will simply render a String when the object is a String. It is probably more common to cast the reference returned by next() to whatever object type you have in your list.

By the way, an Iterator and an Enumeration are essentially the same thing. Iterator is the new and improved version, but many of the legacy classes still use Enumeration.

import java.util.*;
 
class HelloArrayList
{
   public static void main(String args[])
   {
      // This might simulate a raw list read from a text file:
      String rawNames = "Anastasia Bob Chad Dianne Fred George Hugh " +
                        "Ian Jack Kirk Lisa Mandy Ned Oliver " +
                        "Pricilla Quentin Roberta Samwise Tammy Ulf " +
                        "Vera Will Xavier Yancy Zack";
 
      // Build the list of names by parsing the raw data:
      List names = new ArrayList();
      Enumeration parts = new StringTokenizer( rawNames, " " );
      while( parts.hasMoreElements() )
         names.add( parts.nextElement() );
 
      // Now we have a list, lets print it out and see what's in it:
      System.out.println("These are the names in the list:");
      Iterator nameIterator = names.iterator();
   
      while( nameIterator.hasNext() )
         System.out.println( nameIterator.next().toString() );
   }
}
 
 

Flat View: This topic has 3 replies on 1 page
Topic: removing exit permission from security.policy Previous Topic   Next Topic Topic: attaching files

Sponsored Links



Google
  Web Artima.com   

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