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.
5 1821 Laharl 849
Recognized Expert Contributor
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.
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. - public void getmax() {
-
for (ListIterator<Integer> it = list.listIterator(list.size());
-
it.hasPrevious(); ) {
-
Integer max = it.previous();
-
if(list.get(x) > it.previous());
-
x = max;
-
-
}
-
}
-
2. - public void setrich() {
-
Iterator it = bank.keySet().iterator();
-
System.out.println(bank);
-
while (it.hasNext()) {
-
Object key = it.next();
-
if(bank.get(x) > it.previous());
-
x = rich;
-
}
-
}
BigDaddyLH 1,216
Recognized Expert Top Contributor
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
BigDaddyLH 1,216
Recognized Expert Top Contributor
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Graham Parsons |
last post by:
We are experiencing the following on two PCs running WebLogic server
.... even though every other PC in the organisation has no such
problems running the same software.
Anyone who could shed any light on why this may be occuring - well
we'd be very pleased ;-)
The server runs up and then this error happens after about 5-10
minutes of use.
|
by: Johnny |
last post by:
Hi together,
I'm new to XML and have problems with FOP
(http://www.apache.org/dyn/closer.cgi/xml/fop)
When I try to convert my .fo document to .pdf, I get this message:
fo:39:26
master-reference '' for fo:page-sequence matches no simple-page-master
or page-sequence-master
|
by: Boris Ammerlaan |
last post by:
This notice is posted about every week. I'll endeavor to use the same
subject line so that those of you who have seen it can kill-file the
subject; additionally, Supersedes: headers are used to ensure that
only one copy resides on a given news server.
This notice was last updated on September 7th, 2000, and is available
(with a complete revision history) on the World Wide Web at
http://www.stack.nl/~boris/HTML/ciwahfaq.html. Most of it...
|
by: Limpor |
last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can someone helps me.
Thanks!!
-----------------------------------------------------------------------
The code for the Stock class:
-----------------------------------------------------------------------
public class Stock
{
private Deck deck = new...
|
by: glenn123 |
last post by:
Hi, i am just about out of time to produce a working jukebox which has to perform these functions: to play music files when a track is chosen from a list which when the user presses the change genre button the list is populated with a list of that genre.
I have got the interface done to satisfaction, my problem is that when i press the change genre button nothing happens and when i select a track to play from the list which is setvisible and...
| |
by: smileskhan |
last post by:
Hay Friends...
Here I started a new and interested thread. I hope you also enjoy it. I got a task to creat a Hospital Database in Java. But I don´t have any good clues abouts it. So I also try my best to do it well. But here I posted it for friends too. If you have any perfect idea or correct code about it, so Please send it. I shall wait for your nice comments and help.
Here I also explain about my task..........Hospital...
|
by: carlos123 |
last post by:
Ok, im having EXTREME problems, with arraylists, i simply dont understand them, yes i have read your article. Would someone just take a look at this code, i know it sucks. Look at the areas that say "HELP!!!" next to them. But basicly i want to take data from textfields and put them into an arraylist. So the user inputs data, name, address, age all that, hits save (This is when it goes into the arraylist) the textfields clear, and they are ready...
|
by: DonE |
last post by:
Help me please. I am having problems with my homework assignment. We are to create a product class that hold the item number, the name of the product, the number of units in stock, and the price of each unit. Here is my problem. I have written the program ( I use netbean) and I have encountered plenty of errors. The error messages that I get are as follows:
illegal start type
cannot find symbol
operator cannot be applied
assignment to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |