How to call list objects inside another class
I have a list object inside a class named Scorer.java
and I want to import aLinks
and bLinks
objects of another class named MyParser.java
to get the print aLinks.size()
and bLinks.size()
I tried to import the class first call the object first, but I couldn't do it. Can you tell me how to import list objects from a class to another? Here are the classes:
My Scorer.java
:
public class Scorer {
public static List<LinkNode> score(LinkNode sourceLink, List<LinkNode> links){
List<LinkNode> aLinks = new LinkedList<>();
List<LinkNode> bLinks = new LinkedList<>();
for (LinkNode link : links) {
if(isbLink(sourceLink, link)) {
bLinks.add(link);
} else {
aLinks.add(link);
}
}
My other class MyParser.java
public class MyParser {
private static final String[] PARSE_TAGS = new String[]{"a[href", "area[href"};
public static List<LinkNode> parse(LinkNode inputLink){
Set<LinkNode> outputLinks = new LinkedHashSet<>();
try {
Document parsedResults = Jsoup
.connect(inputLink.getUrl())
.timeout(READ_TIMEOUT_IN_MILLISSECS)
.get();
String tag;
Elements elements;
List<LinkNode> result;
//a[href
tag = "a[href";
elements = parsedResults.select(tag);
result = toLinks(inputLink, elements, tag);
outputLinks.addAll(result);
// I want to print `aLinks.size()` and `bLinks.size()` here
}
return new LinkedList<>(outputLinks);
}
source to share
Just call
MyParser myparser = new MyParser ();
myparser.parse(scorerInstance.getAlinks());
myparser.parse(scorerInstance.getBlinks());
Where scorerInstance
is an instance of FlashScorer. You must add getters for aLinks
and bLinks
to the MyScorer
Class like this:
public List<LinkNode> getALinks() {
return aLinks;
}
public List<LinkNode> getBLinks() {
return bLinks;
}
source to share
You must declare the required variables as global and public
as well static
in the Scorer
class
public class Scorer {
public static List<LinkNode> aLinks = new LinkedList<>();
public static List<LinkNode> bLinks = new LinkedList<>();
public static List<LinkNode> score(LinkNode sourceLink, List<LinkNode> links){
for (LinkNode link : links) {
if(isbLink(sourceLink, link)) {
bLinks.add(link);
} else {
aLinks.add(link);
}
}
}
}
Then in the class, MyParser
call class_name. (dot) static_variable.
Then try this in a class MyParser
:
System.out.println(Scorer.aLinks.size()+ " " + Scorer.bLinks.size());
source to share
First of all, aLinks
and bLinks
should be a member of the Scorer
class and should not be declared inside a class method.
Like this:
public class Scorer {
public List<LinkNode> aLinks = new LinkedList<>();
public List<LinkNode> bLinks = new LinkedList<>();
}
Second, you have to get the object Scorer
in the class MyParser
. Then you can access aLinks
and bLinks
. It should be as follows:
Scorer myScorer = new Scorer();
System.out.println(myScorer.aLinks().size());
System.out.println(myScorer.bLinks().size());
This should be added instead of a comment in the section MyParser
.
Third, if the score
method is static, then you must have an instance of the score
class in your score
method as well. You can pass this instance to a method score
. Like this:
public static List<LinkNode> score(LinkNode sourceLink, List<LinkNode> links, Score myScore){
//myScore.aLinks or myScore.bLinks can be used here
}
source to share