473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList method help?

1 New Member
Hi there
great sight iv been reading and learning from alot of the questions here.
iv just started programming using java... we use Bluej at uni.
my problem is as follows:
i have 2 classes. the 1st class is called soldiers and creates objects called soldiers and stores their attributes, eg name, id number, rank etc...
The second class is taskForce which stores which has an add method and adds soldiers to the task force by storing them in an arrayList
the method i need to create now is a list method but the list method must only list those soldiers which share the same attribute, eg rank
The only way i know how to list some thing is:
public void listSoldiers()
{
System.out.println(toString());
}

im presuming something like:
public void ListByRank(String sRank)
{

for(Soldier soldier : taskForce)
{

System.out.println(toString());

}
}

not sure on the rest if some one could point me in the right direction it would be a great help.
Oct 23 '07 #1
2 1497
r035198x
13,262 MVP
Hi there
great sight iv been reading and learning from alot of the questions here.
iv just started programming using java... we use Bluej at uni.
my problem is as follows:
i have 2 classes. the 1st class is called soldiers and creates objects called soldiers and stores their attributes, eg name, id number, rank etc...
The second class is taskForce which stores which has an add method and adds soldiers to the task force by storing them in an arrayList
the method i need to create now is a list method but the list method must only list those soldiers which share the same attribute, eg rank
The only way i know how to list some thing is:
public void listSoldiers()
{
System.out.println(toString());
}

im presuming something like:
public void ListByRank(String sRank)
{

for(Soldier soldier : taskForce)
{

System.out.println(toString());

}
}

not sure on the rest if some one could point me in the right direction it would be a great help.
Let's see your Soilder class.
And please use code tags when posting code.
Oct 23 '07 #2
JosAH
11,448 Recognized Expert MVP
I assume that you have a list that contains all types of Soldiers:

Expand|Select|Wrap|Line Numbers
  1. List<Soldier> Soldiers= new ArrayList<Soldier>();
  2.  
I also assume that you want to iterate over that list and if a Soldier fits a certain
filter it is printed, otherwise it is ignored:

Expand|Select|Wrap|Line Numbers
  1. void list(Filter filter) {
  2.    for (Soldier soldier : Soldiers)
  3.       if (filter.fit(soldier))
  4.          System.out.println(soldier);
  5. }
  6.  
So, also assuming a Filter interface exists like this:

Expand|Select|Wrap|Line Numbers
  1. public interface Filter {
  2.    public boolean fit(Soldier soldier);
  3. }
  4.  
All you have to do now is implement a few simple classes that implement the
Filter interface and you're in business. Note that I implemented the rest of the
code assuming nothing about the other classes, i.e. even a Soldier can be an
interface as the Filter is an interface. It's a general design rule: "code to the
interface, not the implementation (class)". It makes your life easier and your
code more flexible.

kind regards,

Jos
Oct 23 '07 #3

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

Similar topics

2
11597
by: Anchor | last post by:
Hi All, I was hoping someone could help me out. I am new to VB.NET and I don't know the language too well. I am trying to add an object instance of a class to an array like in the code snipet...
4
3863
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
10
3567
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
4
1709
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties...
1
5564
by: Sylvain | last post by:
Hi, I'm encountering a very simple issue with ArrayList constructor and AddRange() method overriding. I'm defining a class that extends ArrayList and contains one overriden method:...
10
1497
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
14
2543
by: Mike | last post by:
I had a question about threading and access to private class variables. I am developing a windows service which has a class inside of it which will receive various asynchronous calls to it via...
18
4705
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
0
960
by: kinkay716 | last post by:
Hello - Im looking for some clarification on ArrayLists for a project I'm working on. It is basically a program which allows you to create a custom deck of cards. There is a listbox of available...
5
2997
by: blt51 | last post by:
I need to write a program that handles a bank account and does a number of transactions using an arraylist. However, I'm having trouble getting the arraylist to store all the transactions and then...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
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,...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
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...
0
1417
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.