Results 1 to 7 of 7
  1. #1
    pilik is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Printing out arraylist of objects?

    Hey, I have created an arraylist and filled it with objects, now what I want to do is compare the first value of each object against an incremental counter. If it's the same do something if not do something else, along the lines of the first block of code, but I can't seem to find a way of accomplishing this. I'm quite new to java so arraylists full of objects aren't my strong point unfortunately. Cheers for any help.

    Java Code:
    size = 16;
    for(int i = 0; i < size; i++){
    
          if(i == pair.getX()){ //if the first x value in the array is equal to i
            System.out.print(pair.getY() + "  "); print out the coresponding y
          } else { //if they do not match print 0.
            System.out.print("0" + "  ");
          }
        }
    The expected output from this is:
    0 0 0 25
    13 0 0 0
    2 0 0 0
    12 0 0 15


    Here's my current code:

    Java Code:
    import java.util.*;
    
    public class Example{
    
      ArrayList<Pair> ar1 = new ArrayList<Pair>();
    
      public static void main(String args[]){
        Example example = new Example();
        example.fillArray();
    
      }
    
      public void fillArray(){
            ar1.add(new Pair(3, 25));
            ar1.add(new Pair(4, 13));
            ar1.add(new Pair(8, 2));
            ar1.add(new Pair(12, 16));
            ar1.add(new Pair(15, 32));
    
    
        for(Pair pair : ar1){
                System.out.println("xVal: " + pair.getName() + ", yVal: " +pair.getMarks());
            }  
      }
    
      class Pair{
        int x = 0;
        int y = 0;
    
        public Pair(int a, int b){
            this.x = a;
            this.y = b;
        }
    
         public int getName(){
            return this.x;
        }
       
        public int getMarks(){
            return this.y;
        }
    
      }
    }
    Last edited by pilik; 03-13-2013 at 01:19 PM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: Printing out arraylist of objects?

    Your code doesn't compile. It needs a public void fillArray() { at line 13.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Printing out arraylist of objects?

    Java Code:
     public static void main(String args[]){    Example example = new Example();
        example.fillArray();
     
      }
     
       
            ar1.add(new Pair(3, 25));
            ar1.add(new Pair(4, 13));
            ar1.add(new Pair(8, 2));
            ar1.add(new Pair(12, 16));
            ar1.add(new Pair(15, 32));
     
     
        for(Pair pair : ar1){
                System.out.println("xVal: " + pair.getName() + ", yVal: " +pair.getMarks());
            }  
      }
    A couple of observations:

    1. Where is the fillArray() method?
    2. Your ar1.add statements and the for statement are not within any method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    pilik is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Re: Printing out arraylist of objects?

    Sorry guys must have changed something while I was editing it, whole code is in the OP now and compiles.

  5. #5
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Printing out arraylist of objects?

    where is you example.fillArray(); method.you call this method only in your main method but where is the definition of that method?
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: Printing out arraylist of objects?

    Instead of editing your original post, you should paste new information. Your edit makes both of our replies look useless since they are based on different code than the one shown.

    Now it compiles, and no errors are given, but the output is not the desired output. To be honest, I have no idea what the output is supposed to be. 0 0 0 25? What's the logic behind it?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Printing out arraylist of objects?

    In your test loop, you need something like,

    Pair pair = ar1.get(i);

    And make certain to use println when printing the Y value;

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Help Printing an ArrayList
    By RSH in forum New To Java
    Replies: 0
    Last Post: 09-12-2012, 04:20 AM
  2. Problem printing ArrayList
    By pamm in forum New To Java
    Replies: 2
    Last Post: 09-13-2011, 06:17 PM
  3. Replies: 9
    Last Post: 08-19-2011, 12:59 AM
  4. About printing Arraylist on JSP page....
    By vaibhavspawar in forum Advanced Java
    Replies: 0
    Last Post: 08-13-2010, 06:52 AM
  5. ArrayList printing
    By tommyyyy in forum New To Java
    Replies: 4
    Last Post: 03-20-2009, 04:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •