Help with List/Map java Problems please!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gazgump
    New Member
    • Feb 2008
    • 2

    Help with List/Map java Problems please!

    I'm doing some practice questions and cant seem to work out these collection probelms think i need to use iterator or a while loop but cant seem to get them to work - dont really know how i'd go about getting the highest value in the collection any help would be appreciated, even just learning from the answers would be a lot of help.. cheers - see below

    [1] Given a list of integers and an integer variable declared like this:
    List<Integer> list;
    int max;

    and assuming that some values have been added to the list, write a loop which finds the largest value in list and stores it in max.


    [2] A bank account is stored as a Map which maps the names of the account holders to a value representing the current balance of their account:
    Map<String,Inte ger> bank;

    Given a String variable declared like this:
    String rich = null;

    write a loop which will store the name of the person with the most money in their account in rich.


    [3] Using the same bank account as in the previous question, and a list of strings:
    Map<String,Inte ger> bank;
    List<String> names;

    write a loop which fills the list names with the names of all account holders whose accounts are overdrawn, i.e. the balance of their account is less than zero.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    We're not just going to give you the answers. That said, useful things to look at are ListIterator and Iterator , as well as the listIterator() and iterator() methods of the various collection classes.

    As for getting the maximum, do it with a simple array on paper first, then translate your logic to code.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Those seem like easy questions. Do you have specific questions about them? You should post your code and ask specific questions about it. Have you read Sun's Collection Tutorial? http://java.sun.com/docs/books/tutor...ons/index.html

      Comment

      • gazgump
        New Member
        • Feb 2008
        • 2

        #4
        I am just starting to look at java code so i appreciate the link and help, this is what i've come up with for the first two but they dont compile -

        1.

        [CODE=Java]public void getmax() {
        for (ListIterator<I nteger> it = list.listIterat or(list.size()) ;
        it.hasPrevious( ); ) {
        Integer max = it.previous();
        if(list.get(x) > it.previous());
        x = max;

        }
        }
        [/CODE]
        2.

        [CODE=Java]public void setrich() {
        Iterator it = bank.keySet().i terator();
        System.out.prin tln(bank);
        while (it.hasNext()) {
        Object key = it.next();
        if(bank.get(x) > it.previous());
        x = rich;
        }
        }[/CODE]
        Last edited by BigDaddyLH; Feb 22 '08, 04:46 PM. Reason: added code tags

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Please enclose your posted code in [code] tags (See How to Ask a Question).

          This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

          Please use [code] tags in future.

          MODERATOR

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            When you have a syntax error and your code doesn't compile, don't just leave us guessing by merely writing that "it doesn't compile". Supply the exact compiler message and indicate which line generated the error.

            Comment

            Working...