Accessing ArrayList Of Another Class

Oct 19, 2014

What I want to do is this, this is my first class:

public class Footballer {
int goals;
String surname= "";
String team="";
private static int counter=0;
private int dres;
}

(this is just the header of the class... And this is my second class, which contains an ArrayList of the first class:

public class FootballTeam{
String teamname="";
String league="";
ArrayList<Footballer> f;
}

And this is my third class which contains an ArrayList of the second class:

public class FootballLeague{
String leaguename="";
ArrayList<FootballTeam> ft;
}

What I want to do is, know how many of footballers are there in the league? Meaning how many of "f"s are in the "ft"... I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.

I'm trying this method to get the size of the array in the 2nd class, from the 3rd class (containing an ArrayList of classes of 2nd class, but no luck:

int counter=0;
for(int i=0;i<this.ft.size();i++) {
counter+=this.ft[i].f.size();
}

I'm getting this: Array required, but ArrayList<FootballTeam> found ---

View Replies


ADVERTISEMENT

Accessing ArrayList Of Another Class

Oct 19, 2014

What I want to do is this, this is my first class:

Java Code:

public class Footballer {
int goals;
String surname= "";
String team="";
private static int counter=0;
private int dres;
} mh_sh_highlight_all('java');

(this is just the header of the class, just how it looks)...

And this is my second class, which contains an ArrayList of the first class:

Java Code:

public class FootballTeam{
String teamname="";
String league="";
ArrayList<Footballer> f;
} mh_sh_highlight_all('java');
And this is my third class which contains an ArrayList of the second clas:
Java Code: public class FootballLeague{
String leaguename="";
ArrayList<FootballTeam> ft;
} mh_sh_highlight_all('java');

What I want to do is, know how many of footballers are there in the league? Meaning how many of "f"s are in the "ft"... I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.

View Replies View Related

Accessing Values From ArrayList

Nov 27, 2014

I'm a total newbie to Java, and until now all I've done was draw some shapes and flags. I'm struggling to understand the code I've been given. I need to access values stored in an ArrayList within another class. Here are the two classes Seat and Mandate:
 
package wtf2;
import java.util.*;
public class Seat {
public int index;
public String place;
public int electorate;

[Code] ....

The main class contains code that feeds data from 2 text files into Seat and Mandate. From there I managed to access the date in Seat (at the end):

package wtf2; 
import java.io.*;
import java.util.*;
public class CW2 {
public static void main(String[] args)throws Exception {

[Code] ....

Now,instead of getting just the mp for Edinburgh South I need to get the vote values, compare them to each other, take the second biggest and display the associate party value. How to access data from that Array to get started at least.

View Replies View Related

Accessing Object Methods Within Arraylist

Oct 14, 2014

Scanner in = new Scanner(System.in);
ArrayList<rand> selectedRand = new ArrayList<Rand>();
selectedRand.add(new Rand(in.nextLine()));

I have created the most minimal code for creating an array list. I was wondering what the basic syntax of accessing objects methods that are within an Array List. So if I was to trying and get a method such as [.returnValue,] how would this look within a Rand object that is declared in a Array List Since you cannot simply declare a new Rand object and say:

newRandObject.returnValue();

And you must go through the actual slotted portion of the array list. I have searched the web and my text book for an example however none are provided.

View Replies View Related

Accessing ArrayList Elements (Integer / String) And Put Them Into Hashtable

Mar 4, 2014

I have a data structure such as: ArrayList<ArrayList<Integer,String>

The data looks as [[1,2,3] [3,4] [99,98,40,32,45,65,1]]

I am trying to access each element and put them into a hashtable such as:

Hashtable<Integer,String>

With what I am doing below I am getting an out of bound error but can't see any other way of accessing the element

public static void hash(ArrayList<ArrayList<String>> list)
{
for(int i = 0; i < list.size(); i++)
{
int cnt = 0;
for(int y = 0; y < list.size(); y++)
{
if (! hashMap.containsValue(list.get(i).get(y)))
{
hashMap.put(cnt, list.get(i).get(y));
cnt++;
}
...

View Replies View Related

Accessing Arraylist Member Object Methods In Enhanced For Loop?

Feb 24, 2014

I've tried a couple ways to do it, and they don't work. I'm aiming for functionality like I got with the regular for loop, but from an enhanced for loop. Is this simply beyond the scope of an enhanced for loop, or am I just not getting the right syntax?

TestObject to1 = new TestObject("first", 11);
TestObject to2 = new TestObject("second", 12);
TestObject to3 = new TestObject("third", 13);
TestObject to4 = new TestObject("fourth", 14);
TestObject to5 = new TestObject();
List<TestObject> testList;
testList = new ArrayList<TestObject>();

[code]....

The TestObject class is simply an int and a String, with getters getInt and getString. It all works fine with the regular for loop.

edit: I should probably mention that I know what I have in the enhanced for loop now will only display the class name and the hash. I've tried adding the .getString and .getInt, and tried a few other ways to make it work. I just reverted to this because it compiles and runs

View Replies View Related

Counts Ticket Objects In ArrayList - Accessing Private Fields

Nov 4, 2014

I am trying to create a method for my "ticketmachine" object that counts ticket objects in an arraylist of "ticket" objects with a specified field "route". The method works if i change the field type of the ticket class to public, however according to the homework task "route" of the ticket class needs to remain a private field.

Here is my code for the method.

public int countTickets(String route) //HAD TO CHANGE Ticket class FIELD "ROUTE" TO PUBLIC!!!!
{
int index = 0; //returns the number of Ticket objects in the machine with the specified "route".
int ticketCounter = 0;
while (index < tickets.size())

[Code] ....

Is my general approach to the problem wrong? also is there a way to access a private field?

View Replies View Related

Accessing Parent Class Method Using Child Class Object?

Feb 4, 2015

I want to know is there any way we can call parent class method using child class object without using super keyword in class B in the following program like we can do in c++ by using scoop resolution operator

class A{
public void hello(){
System.out.println("hello");
}
}
class B extends A{
public void hello(){
//super.hello();
System.out.println("hello1");

[code]....

View Replies View Related

Accessing A Method From Another Class

Apr 1, 2014

I have an admin class that needs to access a method of another class and I'm unsure how to do it.

One of the methods in the admin class (DancerAdmin) accesses a .txt file with information in and each line is to be extracted and is to be created as an object of the Dancer class. The information in each line is to then be used to set the variables in the Dancer class.

To set the values the Dancer class has setter methods which I need to access each time a new object is created while cycling through the .txt file. I'm struggling to access these methods from the DancerAdmin class when I run the relevant method.

The snippet of code I have from the method in DancerAdmin is

while (bufferedScanner.hasNextLine()) {
currentLine = bufferedScanner.nextLine();
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
dancer.add(new Dancer());
Dancer.setName(lineScanner.next()); mh_sh_highlight_all('java');

I get an error saying non static method setName cannot be referenced from a static content?

View Replies View Related

Accessing Variables Within A Class?

May 19, 2013

I wanna know which is the best way to access a variables within a class ...

Using direct access or using the variable accessor ...

Example :

public class Numbers{
        private int n1;
        private int n2;
     public void setN1(int n){
     n1 = n;

[Code] ....

View Replies View Related

Accessing Static Method In Another Class

Apr 6, 2014

I am getting an error trying to access a static method of another class...theyre both in the same package, I've tried importing the class.

I've tried to do A b=new A()
and then
b.evaluate();

Everything that I try I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: B$A
Caused by: java.lang.ClassNotFoundException: B$A
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Code :

public class A{
public static String evaluate(String op) {
}
}
public class B{
String output=A.evaluate(input);
}

View Replies View Related

Accessing Object Created In Another Class

Mar 10, 2014

I have a situation where I have 2 classes and an array of objects which are causing me trouble.

The object type is one I have created - it is made from a class which is neither of the 2 classes I previously mentioned.

The array is created and occupied in Class1 and the problem arises when I try to reference one of the element from Class2.

At first I forgot the the array would be local to Class1.main so I made the array a global variable using:

Java Code: public MyObjectType[] myArray; mh_sh_highlight_all('java');
Then I tried accessing an element (2) from Class2 using:

Java Code: Class1.myArray[2] mh_sh_highlight_all('java');
However I get errors saying that I can't access the static variable from a non-static context.

I understand a little bit about static and non-static objects/methods but don't know how to fix this. Do I need to include "static" in the array declaration?

View Replies View Related

Accessing Private From Driver Class

May 14, 2015

I came across the below

1) When a variables are declared "Private" How should it be accessed from the driver class ? Sometimes i get an error in driver class saying "your variable is declared Private" why am I getting this error ...

The document says "Private" declared variables should be accessed only through methods. What does that mean.

View Replies View Related

Accessing String Values In Class

Apr 9, 2014

What does the line Console[] conList mean? How am I going to access the string values in class consolelist?

class ConsoleInfo

private String conTitle;
private double conPrice;
private int conQty;
private String conPic;
 
[Code] .....

View Replies View Related

Accessing Variables From Array Instance Of Class?

Jan 26, 2015

I am having some problem accessing variables from an array instance of a class. Heres what i have done;

In the main class:

Example obj[]= new Example[4];

In the main class constructor:

obj[0] = new Example(0);
obj[1] = new Example(1);
obj[2] = new Example(2);
obj[3] = new Example(3);

In the main update() method:

if(condition)
//update

In the Example class constructor:

private boolean change = false;

In the Example class update() method:

if(x >20)
change= true;

Now, i want to access the variable change from the main class, how do i do it? The 'condition' in the if statement is the condition of wether the change variable ia true or false. How do i access it?

View Replies View Related

Accessing Private Field Of Derived Object In Base Class?

Apr 1, 2013

I have this piece of code I wrote a while ago to test something. The issue is accessing a private field of Base class in Base but of a Derived object.

Here is the code:
class Base
{
private int x;
public int getX()

[Code]....

The commented code does not work but casting d to Base does.

Forgot to mention that the compilation error is that x has private access in Base.

View Replies View Related

Accessing One Class File From A Fragment Or A MainActivity File

Dec 4, 2014

I have several different PopupWindows that i want to access from the same fragment. I would like for each PopupWindow to be it's own java file. Here is trimmed down code from an example showPopup.java file,

showPopup.java
public class showPopup {
//public class showPopup extends Activity implements View.OnClickListener{
//public class showPopup extends Window {
public static Context appContext;
PopupWindow popupWindow;
SQLiteDatabase db;
EditText edSpeciesLookup,edSpeciesLookupRowid;

[code]...

How do I configure inheritance to allow showPopup() class to reside in it's own Java file and be called from another class (file)? I've reviewed several hours of online resources as well as my reference books with no productive illumination. I've tried a number of possible implementations and extensions such as "public class showPopup extends Activity implements View.OnClickListener{...", and "public class showPopup extends Window.

View Replies View Related

Value Inherited Between Different Arraylist Of Same Class

May 14, 2014

I have extracted relevant parts of code and outputs, this is the only relevant code for this problem. There is a basic class called SalonData, which contains the fields refereed to. Original data rs. is obtained from an SQL extract, that is populating correctly.

public ArrayList<SalonData> salons=new ArrayList<SalonData>();
ArrayList<SalonData> loc2=new ArrayList<SalonData>();
SalonData salon=new SalonData();
salon.CardRecordID = rs.getString("CardRecordID");
salon.SalonName = rs.getString("Name");

[Code] ....

Printed results after this code is run;

Salons
2022XYZ PTY33 Smith Street319603 9773 2000

loc2
2022XYZ PTY33 Smith Street319603 9773 2000

The code was inteded to populate the phone number in arraylist "loc2", but based on the code, the phone number in arraylist "salons" for Location 2 should still be blank. Question is how did the phone number for this customer in arraylist salons, for Location 2, get populated?

View Replies View Related

How To Add And Remove From Arraylist Through Different Class

Mar 13, 2014

Right now i am working on some java using BlueJ and trying to insert and remove data from an arraylist through a public class or from a new class.

I want it to have it as like a database were you can add and remove data but have a limit of storing.

View Replies View Related

Using ArrayList In Another Class (Main)

Mar 27, 2014

If I declare and ArrayList as follows

"public static ArrayList<Media> mediaList = new ArrayList<Media>();"

How can i access it in another class (main class)?

package Projekt;
import Projekt.Media.rating;
public class MainTest {
static MediaHandler myreg ;
public static void main(String[] args) {
myreg.addMovie(title, playTime, year, seen, directory, path, rating.four, quality, subtitles, language, writer);
}
}

I also have the variables declared but didn't include them for readability.

View Replies View Related

Sorting ArrayList Of A Class According To A Field

Oct 13, 2014

we have an Arraylist<Tweet>, and this class is defined as followe: public class Tweet implements Comparable<Tweet>, Serializable. now if we implement the method comparteTo, then will it be sorted automatically? I want to sort this array by any insert.

View Replies View Related

Difference Between ArrayList And Vector Class

Jun 26, 2014

What is the difference between ArrayList and Vector class

View Replies View Related

Error - ArrayList Has Private Class

Jan 30, 2015

Java Code:

import java.util.Scanner;
import java.util.ArrayList;
public class Problem1
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();

[Code] ....

There is an error and says that my ArrayList has private access. I can't figure out how to fix it.

The code runs but when I enter "Quit", the program just stops. The arraylist isn't printed out?

View Replies View Related

Swing/AWT/SWT :: Adding Arraylist To Jtable In Another Class

Jan 23, 2015

I'm trying to fill my jtable with an arraylist. The problem is the jtable is in an extended class and the arraylist in the mainGUI. Now how can I fill the jtable with the arraylist?

That's the arraylist in my MainGUI

BufferedReader in = null;
ArrayList<String> data = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("1.dat.txt"));
String str;
while ((str = in.readLine()) != null) {
data.add(str);

[Code] ....

View Replies View Related

Populating ArrayList Using Scanner Class Logic Error

Sep 12, 2014

My code runs and populates an arraylist. However my break statement, while stopping the loop ends up being added to the arraylist. And I'm not sure how to fix this error.

public static void main(String args[]) throws Exception
{
// declaring variables
String input = "";
// creating array list
ArrayList<String> nameList = new ArrayList<String>();

[Code] ....

View Replies View Related

Create Own ArrayList Using Collection Without Implementing ArrayList Itself

Feb 28, 2014

I'm trying to create my own arraylist using Collection. My program doesn't do anything. Obviously, I haven't a clue.

import java.util.Collection;
import java.util.Iterator;
public class MyArrayList<T> implements java.util.Collection<T> {
private int size = 4;
private T[] mArray;
public MyArrayList(String[] args) {

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved