I'm a beginner in Java and I struggle a bit. I have 3 classes and I want the ArrayList from one class to access a method from another class. How can I do that?
Here is the method I want to retrieve the method from:
private void markAsUpdated()
{
this.needsUpdate = true;
}
Here is the arrayList:
public int getNumberOfUpdatedSites()
{
for (int i = 0; i < webSite.size(); i )
{
if (webSite.get(i))
{
}
}
Both code belong to two different classes. I'm stuck at the iteration part. Basically it needs to return all the sites that are already updated. I have a UML diagram if needed I can provide it. Thanks in advance.
CodePudding user response:
Assuming that Website
class is as follows:
public class Website {
private boolean needsUpdate;
public boolean isNeedsUpdate() {
return needsUpdate;
}
public void markAsUpdated() {
this.needsUpdate = false;
}
}
Then in your other class, you just need to call the public isNeedsUpdate
method to check if it needs to be updated or not. This means that in your other class you can do as follows:
public int getNumberOfUpdatedSites() {
int updatedWebsitesCount = 0;
for (int i = 0; i < webSite.size(); i ) {
if (!webSite.get(i).isNeedsUpdate()){
updatedWebsitesCount ;
}
}
return updatedWebsitesCount;
}
CodePudding user response:
Easly
class A
{
ArrayList[String]values=new ArrayList[]();
public A(){
//initialize the arraylist
values.add("Hello");
values.add("world");
values.add("from");
values.add("mexico");
}
}
OPTION 1
public class B extends A
{
public B(){
console.write(values.toArray().toString());
}
}
OPTION B
public class B
{
public B(){
A miInstanceOfA=new A();
//get values of ArrayList
console.write(miInstanceOfA.values.toArray().toString());
}
}