?
Solved

Removing Item from Array List

Posted on 2005-03-14
3
Medium Priority
?
215 Views
Last Modified: 2010-03-31
Greetings!

I'm having some trouble with my code .. I am trying to suppress the repetitions of a department name in an array list.

I am setting a boolean within the if statement, to set isDUPLICATE=false when deps.equals(depL.departmentName())

However, whenever I set the boolean, it sets it for the entire function, rather than just within the if statement ..

Basically, I would like it to only be set for the conditional when the criteria is met .. and I want the boolean to be reset to true afterwards (in order to show the rest of the list of depts).  I have NSLOg.out.appendln(Printf) statements everywhere showing the correct boolean values, but for some reason all the values being returned on my HTML page are all either true or false, instead of set as particular to the values in my printf statements.

Please help!  

This is my relevant code:

public NSArray getAllDepartments(){

   if (results.count()==2){
    Department depFirst = (Department)a.objectAtIndex(0);
    Department depLast = (Department).b.objectAtIndex(1);

   if (!(depFirst .divisionName().equals(depLast .divisionName())){
    if((deps!=null)&&(deps.equals(depLast .departmentName())){
     isDUPLICATE=false;

}
   deps=(depLast.departmentName());

   NSArray originalListWithDuplicates=ms.allObjects();
   EOSortOrdering ordering1=new EOSortOrdering("departmentName", EOSortOrdering.CompareAscending);
   NSMutableArray sortOrdering=new NSMutableArray();
   sortOrdering.addObject(ordering1);
   return EOSortOrdering.sortedArrayUsingKeyOrderArray(originalListWithDuplicates,sortOrdering);
}

}

}
0
Comment
Question by:jmc430
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 13539645
You could wrap the Department object and then create a Set of the wrappers. This will eliminate duplicates automatically:


public class DepartmentNoDuplicatesWrapper {
      private Department department;
      
      public DepartmentNoDuplicatesWrapper(Department department) {
            this.department = department;
      }
      public boolean equals(Object o) {
            Department otherDepartment = (Department)o;
            return department.getName().equals(otherDepartment.getName());
      }
      
      public int hashCode() {
            return department.getName().hashCode();
      }
}
0
 

Author Comment

by:jmc430
ID: 13559281
thanks CEHJ!

=)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13560622
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question