• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing an ArrayList of another class  RSS feed

 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok guys, this may've been posted and asked before, but it's so hard to explain in just a few words, so it's hard as hell to search for it as well!

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



(this is just the header of the class, just to give you the idea of how it looks)...

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


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


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:



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

I really hope I am clear enough, please ask me if you don't get what I'm asking..
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately in Java there is no notion of operator overloading in class components, so trying to access an element in a container object using [] won't work...

You must use the provided methods to access the elements...

Your best bet to solve your problem is to have a look at the API
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Unfortunately in Java there is no notion of operator overloading in class components, so trying to access an element in a container object using [] won't work...

You must use the provided methods to access the elements...

Your best bet to solve your problem is to have a look at the API


I know this sound unfair and bad, but could you please please spare me some time and just write the solution to me? if it's not too long... I've lost 3days now trying to solve this and it's freaking me out
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:I know this sound unfair and bad, but could you please please spare me some time and just write the solution to me? if it's not too long... I've lost 3days now trying to solve this and it's freaking me out

We will lead you do it. We will not write your code for you.

Remember that list.get(0) is equivalent to array[0]. Try changing that in your code first.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:I know this sound unfair and bad, but could you please please spare me some time and just write the solution to me?

Welcome to the Ranch!

Sorry, but that's not how it works around here. CodeRanch is NotACodeMill (←click). We prefer that people work out their problems themselves. We will provide tips and feedback but we will not do your work for you.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote: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.

Imagine reaching into someone's pocket, pulling out their wallet, and counting the money in there. That's basically what you're trying to do. The polite and proper thing to do is to ask the other person, if he/she doesn't mind telling you, how much money they have on them. By the same token, instead of you reaching into other objects and rifling through their attributes, you would create methods on those objects so that they can be politely asked to provide the information. Do you know how to write methods?
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Bashkim Ukshini wrote: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.

Imagine reaching into someone's pocket, pulling out their wallet, and counting the money in there. That's basically what you're trying to do. The polite and proper thing to do is to ask the other person, if he/she doesn't mind telling you, how much money they have on them. By the same token, instead of you reaching into other objects and rifling through their attributes, you would create methods on those objects so that they can be politely asked to provide the information. Do you know how to write methods?


Hehe, that's a great way of explaining it..
I tried adding a method in the 2nd class:
(f being an ArrayList<Footballer>)

Then I'm trying to call this function from the 3rd class, which contains an ArrayList<FootballTeam> variable. still getting an error. I'm trying to call it in a for loop cyclus like this for example:
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are only allowed to use the [] operator on an array object and nothing else...

In Jeanne post she suggested to replace [i] for get(i)...
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:You are only allowed to use the [] operator on an array object and nothing else...

In Jeanne post she suggested to replace [i] for get(i)...


How I'm supposed to access the i-th possition of it then? By using the get function it will return me a whole class... should I save that class in a temporary variable of the same type, and then operate with it, or...?
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, I think I did it...




i'm getting no error using this.. is this correct?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:is this correct?


Maybe you can even answer that question yourself
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this is the best Java forum till now...
Thank you so so so much, I've been loosing 3days with this madness! lol
thank you so much guys, you are the best!!!
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're Welcome...

Come again soon
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to see you got it working!

This is why we don't just write the code for people. You learned more AND you got working code this way .
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I completely got it, I just needed one line of it, to see how it's done, then I would've got it, I just needed the correct syntax. Anyways, your way of doing it, has been proveen many times to be best, as you can hardly forget it that way lol..

Anyways, is there a way for me to rate you guys, or give you some +reputaion or anything? I'd definitely love to do that, honestly! Thanks again!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!