Redundancy Checker : Custom List « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » Custom ListScreenshots 
Redundancy Checker



/*
 *        Code by David Beuze. No rights reserved :) - 2006
 *
 *        contact: smerpy@gmail.com
 */

/*
 *        This code snippet takes a list of objects and checks for any redundancy in the list.
 *        In this example I've taken a list of Integer, but it can be generalized to any kind
 *        of object.
 */

/*Output:
 
 * Oh no! The value 1 is redundant.
 
 * */


import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

public class RedundancyChecker {

  public static void main(String[] a) {
    new RedundancyChecker();
  }

  public RedundancyChecker() {

    Integer[] array = new Integer[5]// Create and
    // array of
    // Integer
    Integer i0 = new Integer(0);
    array[0= i0;
    Integer i1 = new Integer(1)// <--------
    array[1= i1; // |
    Integer i2 = new Integer(2)// |--- redundant
    // values
    array[2= i2; // |
    Integer i3 = new Integer(1)// <--------
    array[3= i3;
    Integer i4 = new Integer(4);
    array[4= i4;

    // transform the array into a List
    List l = Arrays.asList(array);

    // Check the List
    checkForRedundancy(l);
  }

  public boolean checkForRedundancy(List l) {
    ListIterator li1 = l.listIterator()// Make a
    // general
    // ListIterator
    // on the list

    while (li1.hasNext()) {
      // Store the value of the actual first checked
      // element of the List,
      // it needs to be stored because it calls the
      // .next(), which we can call only once per loop
      // in order to sweep the whole list.
      int check1 = ((Integerli1.next()).intValue();

      // Make a second ListIterator that will start
      // with the element that is just after the
      // actual first checked one,
      // in order to avoid checking several times the
      // same elements.
      ListIterator li2 = l.listIterator(li1.nextIndex() 1);

      while (li2.hasNext()) {
        // Store the following elements one by
        // one and check to see if they match
        // the actual one.
        int check2 = ((Integerli2.next()).intValue();
        if (check1 == check2) {
          System.out.println("Oh no! The value " + check1 + " is redundant.");
          return true;
        }
      }
      // The .next() method has already been called at
      // the beginning of the loop.
    }
    return false;
  }
}
           
       
Related examples in the same category
1. Doubly-linked list with data structureDoubly-linked list with data structure
2. Sorted listSorted list
3. List with first and last referencesList with first and last references
4. Frist last list
5. Another Link list
w_ww___.__j_a___v_a2__s._c_o__m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.