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 16 of 16

Thread: Creating ArrayLists

  1. #1
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Creating ArrayLists

    Hi, I am creating a simple program using Kruskals Algorithm

    I am stuck on creating almost like a structure to hold the data for the algorithm.

    For example, I need an arraylist of:

    edges which contains
    {
    int from;
    int to;
    int weight;
    }

    I have tried many ways but cannot do it. I need to be able to add data like

    e.g. if i have an arraylist called myEdges
    i want to be able to do myEdges.add and be able to add a value for each of the elements inside the edge type.

    I will later need to sort the data by the weight.

    this is what i have done so far

    public class Edge 
    {
    	public int from;
    	public int to;
    	public int weight;
     
    	public Edge(int from, int to, int cost) 
    	{
    	this.from = from;
    	this.to = to;
    	this.weight = cost;
    	}
    }
    	   ArrayList<Edge> myEdges = new ArrayList<Edge>();

    but i am not sure how to add data to it.

    thank you.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Creating ArrayLists

    Instantiate a new Edge using the constructor, and add that instance to your ArrayList.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating ArrayLists

    Sorry could you explain further i am new to java.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Creating ArrayLists

    Do you know how to create an instance of something? Recommended reading: Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating ArrayLists

    Hi, would i need to do something like this

    Edge myEdges;

    ArrayList<Edge> myEdges = new ArrayList<Edge>();

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Creating ArrayLists

    What happened when you tried that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating ArrayLists

    It said duplicate name myEdges

  8. #8
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating ArrayLists

    public class Edge
    {
    public int from;
    public int to;
    public int cost;
    public Edge myEdges;

    public Edge(int from, int to, int cost)
    {
    this.from = from;
    this.to = to;
    this.cost = cost;
    }


    Edge Edge1 = new Edge(1, 2, 3 );



    ArrayList<Edge> edgeList = new ArrayList<Edge>();

    when i type edgeList.
    no options come up like add etc

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Creating ArrayLists

    First off, stop relying on your IDE for options.

    Secondly, is that exactly what your program looks like? Statements like method calls need to be inside a method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating ArrayLists

     
    import java.util.ArrayList;
     
    public class Edge 
    {
    	static ArrayList<Edge> edgeList = new ArrayList<Edge>(); 
     
    	public int from;
    	public int to;
    	public int cost;
    	public Edge myEdges;
     
    	public Edge(int from, int to, int cost) 
    	{
    	this.from = from;
    	this.to = to;
    	this.cost = cost;
    	}
     
     
    public Edge Edge1 = new Edge(1, 2, 3);
     
     
    	public void Data()
    	{
     
    	edgeList.add(0, Edge1);
    	edgeList.add(1, Edge1);
     
    	}
     
     
     
    		public static void main(String[] args)
    		{
    			System.out.print(edgeList.size());
     
    		}
     
    }

    i am printing the arraylist to see if the elements have been added but the arraylist still has a size of 0?

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Creating ArrayLists

    Well, you are adding an Edge (the same Edge) twice in the Data method (which should be data, follow standard naming conventions). But when do you call that method?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating ArrayLists

    Hi, thanks for your help, I got it working.

    Thanks once again.

  13. #13
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Creating ArrayLists

    You definitely have to read a tutorial on the Java Collections Framework to help you understand Lists implementations. After that, you will know why while instantiating an object, you do not need to precede it with the public access keyword as above. Make it easier to understand by separating the two files and use the main method to instantiate the ArrayList class and then add whatever you wanted to. Also, read the Java API Docs for JCF methods.

  14. #14
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Re: Creating ArrayLists

    hi, I hope u r looking for this.
    ...
    Last edited by copeg; January 12th, 2012 at 08:54 AM.

  15. #15
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Creating ArrayLists

    Quote Originally Posted by neeraj0708 View Post
    hi, I hope u r looking for this.
    ...
    neeraj0708, please read the forum rules and the following:
    The Problem with Spoon-feeding

  16. #16
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating ArrayLists

    Hi, thanks everyone for the help but dont worry, I've already done it.

    Many thanks.

Similar Threads

  1. Sprite Animation with ArrayLists
    By FShounen in forum AWT / Java Swing
    Replies: 13
    Last Post: June 6th, 2011, 08:43 AM
  2. Quick question, arraylists
    By Intensity` in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 16th, 2011, 08:47 PM
  3. Creating multiple arraylists with a loop?
    By DLX in forum Collections and Generics
    Replies: 4
    Last Post: December 28th, 2010, 05:58 PM
  4. Encapsulation question regarding ArrayLists
    By LDM91 in forum Collections and Generics
    Replies: 3
    Last Post: October 27th, 2010, 11:51 AM
  5. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 02:13 PM