LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java - How do i modify (add/remove etc.) an ArrayList from outside of the class... (https://www.linuxquestions.org/questions/programming-9/java-how-do-i-modify-add-remove-etc-an-arraylist-from-outside-of-the-class-794385/)

mitchell7man 03-10-2010 02:07 AM

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.

aspire1 03-10-2010 11:31 AM

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.

mitchell7man 03-10-2010 01:30 PM

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?

mitchell7man 03-10-2010 01:36 PM

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

nadroj 03-10-2010 02:02 PM

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.

Larry James 03-10-2010 03:31 PM

edit: replied to wrong thread.... Moderators please delete.

paulsm4 03-10-2010 04:28 PM

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

tamtam 03-10-2010 05:06 PM

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);
}


paulsm4 03-10-2010 05:16 PM

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

tamtam 03-10-2010 05:24 PM

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);


tamtam 03-10-2010 05:29 PM

Quote:

Originally Posted by paulsm4 (Post 3893486)
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.

aspire1 03-10-2010 05:41 PM

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 :)

nadroj 03-10-2010 05:55 PM

Quote:

Originally Posted by tamtam (Post 3893499)
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.

mitchell7man 03-10-2010 09:13 PM

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.!


All times are GMT -5. The time now is 01:39 AM.