Calling an ArrayList of Strings to another class? - java

Am I doing this correctly?
test.java (MAIN):
public class test {
public static ArrayList<String> testArray = new ArrayList<String>();
public static void main (String[] args) {
ArrayList<String> test = new ArrayList<String>();
test.add("blah1");
test.add("blah2");
}
public static ArrayList<String> passTestArray() {
return testArray;
}
}
secondClass.java
public class secondClass {
ArrayList<String> test = test.passTestArray();
for (int i = 0; i < test.size(); i++) {
System.out.println(authors.get(i));
}
}
Getting nothing as an output from the secondClass class. Not sure if I'm doing this correctly or not. :/

In secondClass.java, you're creating a new ArrayList and then trying to call a method on it that doesn't exist. You'll need to do something more like the below to initialize your test class and call its methods.
public class SecondClass {
private Test firstClass;
private ArrayList<String> test = new ArrayList<String>();
SecondClass() {
firstClass = new Test();
test = firstClass.passTestArray();
}
}
In the above, you'll be creating the secondClass.java, initializing your first class (test.java), and then calling its methods.

Related

ArrayList empty

I think it's a simple question but keeps me from moving forward in the project. Why, when I use the ArrayList as a reference from a class to another, it is empty?
public class FirstClass {
private ArrayList<String> myList = new ArrayList<>();
public ArrayList<String> getMyList() {
return myList;
}
public static void main(String[] args) {
FirstClass fc = new FirstClass();
fc.getMyList().add("Test");
System.out.println(fc.getMyList());
}
}
Output when I run FirstClass.main:
[Test]
public class OtherClass {
FirstClass csv = new FirstClass();
public ArrayList<String> otherList = csv.getMyList();
public static void main(String[] args) {
OtherClass oc = new OtherClass();
System.out.println(oc.otherList);
}
}
Output when running the OtherClass.main:
[]
I've Googled so many time and the solutions that I found are not working. I followed step by step, creating a getter method in the class that is populating the ArrayList, use the method to populate it, use the print method to make sure the array is populated at the end of the main method. All good, ArrayList is full and ready to be used but when used as reference is empty.
Because a new instance of FirstClass is being created inside of OtherClass, otherList in OtherClass is empty.
The ArrayList that oc.otherList refers to is different from the one that you populated for FirstClass in the main method.
You can edit your OtherClass to work:
public class OtherClass {
private FirstClass csv;
public ArrayList<String> otherList;
public OtherClass(FirstClass fc) {
this.csv = fc;
this.otherList = fc.getMyList();
}
public static void main(String[] args) {
FirstClass fc = new FirstClass();
fc.getMyList().add("Test");
OtherClass oc = new OtherClass(fc);
System.out.println(oc.otherList);
}
}
OtherClass creates the FirstClass without FirstClass main method, so it does not add() to the list.
You can do something like below.
public class FirstClass {
private List<String> myList;
// Instance block: will executed the moment you create object
// of FirstClass
{
myList = new ArrayList<>();
myList.add("Test String");
}
public List<String> getMyList() {
return myList;
}
}
public class OtherClass {
private static List<String> otherList;
public static void main(String[] args) {
FirstClass fc = new FirstClass();
otherList = fc.getMyList();
System.out.println(otherList); //iterate if want to check specific elements in the list
}
}
Or a better way would be to create a method which will add elements to the list and read it in the otherclass by calling the method using firstclass's object.

How to access the value of string by the inheritance?

import java.util.*;
class Pilot
{
protected String PILOT = "BSIT-1A";
public static void Subject()
{
String[] subs = {"Comprog11","WebDev","Digilog12","ComProg12"};
}
public static void Teacher()
{
String[] teach = {"Ms.a","Ms.b","Ms.c","Ms.d"};
}
}
class Pilot1 extends Pilot
{
protected String PILOT1 = "BSIT-1B";
public static void main(String[]args)
{
Pilot1 obj = new Pilot1();
System.out.println(obj.PILOT);
System.out.println(obj.PILOT1);
obj.Subject();
obj.Teacher();
}
how to display the values of Subject() and Teacher() if I put inside it a String?It doesnt have any compiler issues but when I ran it display only the
BSIT-1A
BSIT-1B
my expected output is
BSIT-1A
BSIT-1B
Comprog11
Webdev
Digilog12
Comprog12
Ms.a
Ms.b
Ms.c
Ms.d
You can return the array
public String[] subject() {
return {"Comprog11","WebDev","Digilog12","ComProg12"};
}
Then remove the inheritance. You don't need it for the main method. Make the pilot constants public or pass those strings into a class constructor and add a private field with a getter method, for example
Pilot a = new Pilot("BSIT-1A");
Pilot b = new Pilot("BSIT-1B");
System.out.println(a.getCode());
System.out.println(b.getCode());
Arrays.stream(a.subject()).forEach(System.out::println);
You need the either print the values in Subject() and Teacher() or make them return the values. Also calling static methods via objects is not a
good practice. They should be invoked by the class, like Pilot1.Subject().
class Pilot
{
protected String PILOT = "BSIT-1A";
public static void Subject()
{
String[] subs = {"Comprog11","WebDev","Digilog12","ComProg12"};
for(String sub : subs){
System.out.println(sub);
}
}
public static void Teacher()
{
String[] teach = {"Ms.a","Ms.b","Ms.c","Ms.d"};
for(String t : teach){
System.out.println(t);
}
}
}

Private and Public variables in a method Java

Say I have a class that uses multiple ArrayLists:
public static ArrayList<String> openable = new ArrayList<>();
public static ArrayList<String> commands = new ArrayList<>();
public static ArrayList<String> mail = new ArrayList<>();
public static ArrayList<String> mailCommands = new ArrayList<>();
public static ArrayList<String> previousCommands = new ArrayList<>();
And I have multiple other classes that contain methods that use those ArrayLists. Say I also have a class called connect, that extends this first class containing those variable:
public class Connect extends Main_Menu{
If I were to call the methods that I usually call in the Main_Menu class, from the Connect class using new variables with the same name that are private:
private static ArrayList<String> previousCommands = new ArrayList<String>();
private static ArrayList<String> openable = new ArrayList<>();
private static ArrayList<String> commands = new ArrayList<>();
private static ArrayList<String> mail = new ArrayList<>();
private static ArrayList<String> mailCommands = new ArrayList<>();
Would the method using these ArrayLists use the data from the public variables in the Main_Menu class or would it use the private data from the variables in the Connect class?
see for yourself:
static class Parent
{
public static int A = 5;
public static int B = 18;
public static int getA(){
return A;
}
public static int getB(){
return B;
}
}
static class Child extends Parent
{
private static int A = 10;
public static int getA(){
return A;
}
}
public static void main(String[] args) {
Parent p = new Parent();
Child c = new Child();
System.out.println("Parent A: " + p.getA());
System.out.println("Parent B: " + p.getB());
System.out.println("Child A: " + c.getA());
}
output:
run:
Parent A: 5
Parent B: 18
Child A: 10
BUILD SUCCESSFUL (total time: 0 seconds)
I suppose that the static fields in the Connect class are merely overriding the visibility of the same fields in the Main_Menu class. Outside of the scope of the Connect class, the field is not visible, and so you can't access it. You would get an exception if you could even compile at all.
Edit: I am assuming that you are accessing these static variables directly, and not accessing private fields through overridden methods as suggested by other users.
Main.java:
public class Main {
public static ArrayList<String> things = new ArrayList<>();
public static void main(String[] args){
Playground.things; // <- Cannot access private static field outside of that class
}
}
Playground.java:
public class Playground extends Main {
private static ArrayList<String> things = new ArrayList<>();
}

Getting and setting an arraylist that's been declared in the main?

Okay, so I have an arraylist declared in my main because elsewhere it brings up an error. However, I want to use that arraylist later specifically to have a getter, but it doesn't recognize the arraylist because it is in my main.
The error is
"it cannot be resolved to a variable"
.
What can I do to correct this?
public static void main(String[] args) {
ArrayList <String> Strings = new ArrayList <String>();
Strings.add("hi");
Strings.add("hello");
Strings.add("goodbye");
}
public ArrayList<String> getArrList() {
return Strings;
}
You need to make use of the OOP, dont define things static if not needed, use setters and getters and encapsulate the private fields of the classs
Example:
public class Tester {
private List<String> stringList;
public Tester() {
stringList = new ArrayList<String>();
}
public void populateList() {
stringList.add("hi");
stringList.add("hello");
stringList.add("goodbye");
}
public static void main(String[] args) {
Tester t = new Tester();
t.populateList();
List<> list = t.getList();
System.out.println( list );
}
public List<String> getList(List<String> list) {
return stringList;
}
public List<String> setList() {
return stringList;
}
}
You can't call non-static method from static method.
You must define your arraylist as a static variable, then make its getter as static.
static ArrayList <String> Strings;
public static void main(String[] args) {
Strings = new ArrayList <String>();
Strings.add("hi");
Strings.add("hello");
Strings.add("goodbye");
}
public static ArrayList<String> getArrList() {
return Strings;
}
Do like this,
class MyClass{
ArrayList <String> Strings;
public static void main(String[] args) {
Strings = new ArrayList <String>();
Strings.add("hi");
Strings.add("hello");
Strings.add("goodbye");
}
public ArrayList<String> getArrList() {
MyClass myClass = new myClass();
return myClass.Strings;
}
}

Output gives nothing

class A contain:
public static ArrayList<String> sourceList = new ArrayList<String>();
// here ArrayList contain some string type data;
public static ArrayList<String> getSource()
{
return sourceList;
}
main class contain:
List<String> s1 = A.getSource();
for(String dk:s1) {
System.out.println(dk);
}
Here I am storing ArrayList data of class A into a list in class B and checking
whether its works or not.but when I run the main class it gives nothing.
thanks.
I've simulated what you are doing, and it's working as expected. Are you sure the list is being populated correctly?
import java.util.ArrayList;
import java.util.List;
public class PrintList {
public static void main(String[] args) {
List<String> s1 = AClass.getSource();
for (String dk : s1) {
System.out.println("We have : " + dk);
}
}
}
class AClass {
public static final ArrayList<String> sourceList = new ArrayList<String>();
static {
sourceList.add("A string");
sourceList.add("Another string");
}
public static ArrayList<String> getSource() {
return sourceList;
}
}
If that is all the code you are using, then you will never get anything since you are never populating the array list in the first place.
If you really want to see if it is working, try something like so:
public static ArrayList<String> sourceList = new ArrayList<String>();
// here ArrayList contain some string type data;
public static ArrayList<String> getSource()
{
sourceList.add("Hello I am working");
return sourceList;
}
If all goes well, you should see the string Hello I am working.

Categories

Resources