By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,291 Members | 3,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,291 IT Pros & Developers. It's quick & easy.

Help with List/Map java Problems please!

P: 2
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,Integer> 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,Integer> 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.
Feb 22 '08 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 849
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.
Feb 22 '08 #2

Expert 100+
P: 1,216
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?
Feb 22 '08 #3

P: 2
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 -


Expand|Select|Wrap|Line Numbers
  1. public void getmax() {
  2.        for (ListIterator<Integer> it = list.listIterator(list.size());
  3.      it.hasPrevious(); ) {
  4.     Integer max = it.previous();
  5.     if(list.get(x) > it.previous());
  6.     x = max;
  8. }
  9. }

Expand|Select|Wrap|Line Numbers
  1. public void setrich() {
  2.         Iterator it = bank.keySet().iterator();
  3.         System.out.println(bank);
  4.         while (it.hasNext()) {
  5.            Object key =;
  6.            if(bank.get(x) > it.previous()); 
  7.                x = rich;
  8.         }
  9.     }
Feb 22 '08 #4

Expert 100+
P: 1,216
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.

Feb 22 '08 #5

Expert 100+
P: 1,216
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.
Feb 22 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.