LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-10-2010, 03:07 AM   #1
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Rep: Reputation: 31
Java - How do i modify (add/remove etc.) an ArrayList from outside of the class...


Okay so I'm working on a program here as I'm learning java,
I have an array that is initialized with 5 objects that are hard coded.
I have made a GUI that takes the input needed and creates an object with those values.
I need to add that object to the ArrayList that I have previously made.

Okay, so I have three classes, guiclass.java, main.java and gladiator.java

Objects are made and defined in "gladiator"
Main contains my public static void main section, launches my gui, creates my five hard coded objects, creates my ArrayList and adds my five hard coded objects to the ArrayList.

Now, I need to add the object that I generated in the guiSection [action Listener]to the ArrayList that I created in my main class's public static void main string... section. Problem is my arraylist "cannot be resolved" from guiclass.

How do I do this?


The ide i'm using is Eclipse.

Thanks for your help.
 
Old 03-10-2010, 12:31 PM   #2
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 61

Rep: Reputation: 22
You could pass a reference to the ArrayList in your GuiClass constructor:
Code:
public class GuiClass{

 private final ArrayList myArrayList;

 public GuiClass( ArrayList myArrayList){
    this.myArrayList = myArrayList;

    //then add things to the ArrayList where ever it's appropriate

 }
}
or make your ArrayList public static although I'd prefer the above method.
 
Old 03-10-2010, 02:30 PM   #3
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Original Poster
Rep: Reputation: 31
I did that, but in my main class the line guiclass panel = new guiclass(); quits working, it expects something in the (), is that going to mess with my program? What is it needing?
 
Old 03-10-2010, 02:36 PM   #4
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Original Poster
Rep: Reputation: 31
Parts of guiclass:

Code:
public class guiclass extends JPanel{
private final ArrayList<gladiator> myArrayList;	
.....
Code:
public guiclass(ArrayList<gladiator> myArrayList)
	{
//Array Declaration
this.myArrayList = myArrayList;
//Five Initial Gladiators Created
gladiator glad1 = new gladiator("Richard", 5);
gladiator glad2 = new gladiator("Dominscus", 8);
gladiator glad3 = new gladiator("Jesuisus", 5);
gladiator glad4 = new gladiator("Mitchus", 9);
gladiator glad5 = new gladiator("Richarduscus", 4);
//Five Initial Gladiators added to myArrayList
myArrayList.add(glad1);
myArrayList.add(glad2);
myArrayList.add(glad3);
myArrayList.add(glad4);
myArrayList.add(glad5);

My Main Class
Code:
import java.util.ArrayList;

import javax.swing.JFrame;


public class main {
	
	public static void main (String args[])
	{			
	JFrame frame = new JFrame("Gladiators");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	guiclass panel = new guiclass();
	frame.getContentPane().add(panel);
	frame.pack();
	frame.setVisible(true);
	}


}
-- i get "the contructor guiclass() is undefined:


portion of guiclass whith listener and where i'm trying to add to the array.
Code:
private class ButtonListener implements ActionListener
{
	public void actionPerformed (ActionEvent event)
	{
		if (event.getSource() == addGlad)
		{
			System.out.print("add gladiator has been clicked.");
			String name = gladName.getText();
			String skillString = gladSkill.getText();
			int skill = Integer.parseInt(skillString.trim());
			gladiator randyName = new gladiator(name, skill);
			myArrayList.add(randyName);

		}
Thanks for your help. MJ
 
Old 03-10-2010, 03:02 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 59
In Java, if you don't explicitly define a constructor, a default, no-arg one is automatically created/assumed. Since you create a constructor
Code:
public guiclass(ArrayList<gladiator> myArrayList)
then no default, no-arg one is automatically created. Therefore, the line
Code:
	guiclass panel = new guiclass();
Is asking for the no-arg constructor, but such a constructor doesn't exist. You'll have to either explicitly create one, or pass the necessary parameter to the constructor you've already defined.
 
Old 03-10-2010, 04:31 PM   #6
Larry James
Member
 
Registered: Jun 2000
Location: Buffalo, New York
Distribution: Fedora, Ubuntu
Posts: 345

Rep: Reputation: 38
edit: replied to wrong thread.... Moderators please delete.

Last edited by Larry James; 03-10-2010 at 04:32 PM.
 
Old 03-10-2010, 05:28 PM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I don't know if this has already been suggested ... but I would just introduce a public "add()" method to my "guiclass()":

Code:
public class guiclass extends JPanel{
private final ArrayList<gladiator> myArrayList = new ArrayList<gladiator>();	
.....
public add (gladiator g)
{
  myArrayList.add (g);
}
GREAT MOVIE QUOTES:
"I am Spartacus" - (not) Kirk Douglas

"I knew a man once who said, "Death smiles at us all. All a man can do is smile back." - Russell Crowe

"Joey - do you like movies about gladiators?" - Peter Graves

Last edited by paulsm4; 03-10-2010 at 07:11 PM.
 
Old 03-10-2010, 06:06 PM   #8
tamtam
Member
 
Registered: May 2004
Distribution: Slackware.
Posts: 321

Rep: Reputation: 33
Hi, just a thought here. What is gladiator (watch the case here). If it is a class then all Classes in Java have to start with an upper-case letter. If you create a class using lower-case then Eclipse should show an error. In your code snippet gladiator and guiclass are both in lower case...
Code:
public class guiclass extends JPanel{
private final ArrayList<gladiator> myArrayList;	
.....
public add (gladiator g)
{
  myArrayList.add (g);
}
Should this not be...
Code:
public class Guiclass extends JPanel{
private final ArrayList<Gladiator> myArrayList;	
.....
public add (Gladiator g)
{
  myArrayList.add (g);
}

Last edited by tamtam; 03-10-2010 at 06:07 PM.
 
Old 03-10-2010, 06:16 PM   #9
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
tamtam -

Upper case/lower case (or, for that matter, .Net "CamelCase") are merely conventions - not rules.

I would *encourage* mitchell7man to capitalize the first letter of all of his classes and, yes, "upper case vs. lower case" is a common source of errors when you're overriding class methods.

*However* ... I believe the OP's class is "gladiator"

IMO .. PSM
 
Old 03-10-2010, 06:24 PM   #10
tamtam
Member
 
Registered: May 2004
Distribution: Slackware.
Posts: 321

Rep: Reputation: 33
Okay, assuming you have imported the relevant packages...

Your ArrayList object you need to initialise...
Code:
private ArrayList<Gladiator> myArrayList = new ArrayList<Gladiator>() 
// creates an empty ArrayList of the class Gladiator
now populate in your Constructor - you don't need a parameter unless your initialising the ArrayList elsewhere
Code:
public Guiclass()
{
//Five Initial Gladiators Created
Gladiator glad1 = new Gladiator("Richard", 5);
Gladiator glad2 = new Gladiator("Dominscus", 8);
Gladiator glad3 = new Gladiator("Jesuisus", 5);
Gladiator glad4 = new Gladiator("Mitchus", 9);
Gladiator glad5 = new Gladiator("Richarduscus", 4);
//Five Initial Gladiators added to myArrayList
myArrayList.add(glad1);
myArrayList.add(glad2);
myArrayList.add(glad3);
myArrayList.add(glad4);
myArrayList.add(glad5);

Last edited by tamtam; 03-10-2010 at 06:38 PM.
 
Old 03-10-2010, 06:29 PM   #11
tamtam
Member
 
Registered: May 2004
Distribution: Slackware.
Posts: 321

Rep: Reputation: 33
Quote:
Originally Posted by paulsm4 View Post
tamtam -

Upper case/lower case (or, for that matter, .Net "CamelCase") are merely conventions - not rules.

I would *encourage* mitchell7man to capitalize the first letter of all of his classes and, yes, "upper case vs. lower case" is a common source of errors when you're overriding class methods.

*However* ... I believe the OP's class is "gladiator"

IMO .. PSM
However lower case first letter in Java is an error. It will not compile otherwise.
Oops, it does compile. When did that happen. Used to be a rule by convention. Must have missed this with the emergence of Java 5 or 6.

Last edited by tamtam; 03-10-2010 at 06:36 PM.
 
Old 03-10-2010, 06:41 PM   #12
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 61

Rep: Reputation: 22
So the original poster wasn't creating his ArrayList in the main method after all, plenty of solutions now though, in both upper and lowercase
 
Old 03-10-2010, 06:55 PM   #13
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 59
Quote:
Originally Posted by tamtam View Post
However lower case first letter in Java is an error. It will not compile otherwise.
Oops, it does compile. When did that happen. Used to be a rule by convention. Must have missed this with the emergence of Java 5 or 6.
Also, a "convention" doesn't mean "specification". For as long as I can remember (well, the ~9 years that I've been using Java), this restriction has never been in place. There are other restrictions on naming of classes and variables, for example, the specification prevents these to start with a number (if I recall correctly), among other things. Most of these restrictions, in my opinion, are only there because it's too difficult, ambiguous, or impossible, for the compiler or JVM to work with. Things like the case of characters really don't cause a problem for it.

Last edited by nadroj; 03-10-2010 at 06:57 PM. Reason: grammar
 
Old 03-10-2010, 10:13 PM   #14
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Original Poster
Rep: Reputation: 31
Thanks for the help.
I moved the ArrayList contructor into guiclass, created the gladiator objects in guiclass, added them to the array in guiclass. And am able to add to the Array from my listener.!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java: what's the difference between an inner class and a static inner class? irey Programming 6 01-28-2009 04:34 AM
Sort test for an ArrayList (java) thefalling Programming 3 12-02-2006 11:32 PM
kdevelop add folder and add new class is slow edwardt Programming 0 03-03-2004 07:04 PM
ArrayList question (Java) ksgill Programming 5 10-14-2003 03:00 PM
Java ArrayList question ksgill Programming 2 10-12-2003 08:54 AM


All times are GMT -5. The time now is 03:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration