Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: loop through linked list of objects and their fields

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default loop through linked list of objects and their fields

    Below I have got a class with two int variables from which I have created a number of objects which I have stored in the linked list. Now I want to loop through each object and compare THEIR FIELDS from the class with other int values. In other words I want to loop through the fields of the objects and not the objects themselves. Any help?

    Here's the code

    import java.util.*;
    import java.io.*;
     
     
    public class Obj 
    {
        int n;
        int c;
     
       public Obj (int nn)
    {
        n = nn;
        c = 0;  
    }
     
     
       public static void main(String argv[]) throws IOException
       {
     
          LinkedList list = new LinkedList();   
     
          int i = 7;
          Obj element = new Obj(i);
          // I may add further objects..
     
         list.add(element);
     
         // then I want to iterate through the linked list of objects and get each object
         // and compare its n or c field values with something else
     
         // It should sth like the below which I found in the web but I don;t get how it works
     
        for(Obj elementf : list) 
        {
           // Process each object inside of object here.
         }
     
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: loop through linked list of objects and their fields

    To obtain the fields from two objects in an ArrayList so that they can be compared:

    // get field1 from one object
    myArrayList.get( index1 ).getField1();

    // get field 1 from another object in the ArrayList
    myArrayList.get( index2 ).getField1();

    The results of those statements can be compared directly or assigned to variables and then the variables can be compared. You haven't described the purpose of the comparison, but if you're trying to determine if two objects are equal based on the values of their respective fields, you should look at writing an equals() method. Or you might also research the Comparable interface, how it works, and either use that or write something similar.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: loop through linked list of objects and their fields

    like the below ... how it works
    That's an enhanced for loop. See the tutorial: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    It generates code to go through the collection or array to get elements and assign them to the variable to the left of the :
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Adding Student Objects into singly linked list alphabetically [NullPointerException]
    By Restivethought in forum Object Oriented Programming
    Replies: 2
    Last Post: March 29th, 2013, 07:37 AM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  3. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  4. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM