Solved

Using my ArrayList from any method

Posted on 2011-09-19
4
305 Views
Last Modified: 2012-05-12
I want to be able to access my ArrayList in any method in my class without having to pass it to the method.  Example would be

public int getRowCount() {
                  int rows = aList.size();
                   return rows;
               } // end getRowSize()

Where Alist is my ArrayList

I am putting ArrayList in the Constructor, but that don't seem to do it.

public SelectedTModel() {
      super();
     ArrayList<RevDataset> aList = EdiSelection.rds;
}
0
Comment
Question by:jkteater
  • 3
4 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36562661
you need to declare it as an intancwe varuiable
and assign in the constructor:
public class MyClass{
ArrayList  a;

MyClass(ArrayList a){

this.a = a;


}


...


Then it will be accessible everuywhere
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36562672

when you do this in the constructior
  ArrayList<RevDataset> aList = EdiSelection.rds;
you aList becvomes a local; varaiable
of the constructorr

You need to assign arrauylit to sthe instance
varaible of your class, then it would be accesible anyware in the class
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 50 total points
ID: 36562673
Create your ArrayList as a class variable something like this:

public class YourClass {
    public int getRowCount() {
        int rows = this.aList.size();
        return rows;
    } // end getRowSize()

    private ArrayList<RevDataset> aList;

    public SelectedTModel() {
        super();
        this.aList = EdiSelection.rds;
    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36562681

when you do this in the constructior
  ArrayList<RevDataset> aList = EdiSelection.rds;

you declaration
 ArrayList<RevDataset> aList
hapopnes inside constructor - so it is local to the constructor

decalartion should be outside any method then it is an instance variable
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 20
print map entry 34 69
windows explorer path to command prompt 5 44
web application structure 18 77
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question