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.
Related
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.
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;
}
}
I want to create an object using a method but I want it (object+reference) to live even after the method ends. Example of code:
public class start{
public static void main (String [] Args){
public void createObject(){
object1 createdObject = new object1();
}
createObject();
createdObject.doSomething();
}
}
public class object1{
//code for object 1
}
So my main question is: how to create object using method and let it live even after method ends. The problem is that reference createdObject is popped of stack after method ends and therefore I can't use it anymore. Is it even possible to create object for further use this way?
public class start{
public static void main (String [] Args){
//class level scope
object1 createdObject = null;
private void createObject(){
createdObject = new object1();
}
}
public class object1{
//code for object 1
}
NOTE: I have not following naming conventions. But please follow them in actual code
UPDATE: Proper code check it out
public class Main {
public static void main(String[] args) {
new MyClass().doSomething();
}
}
class MyClass{
Object obj ;
public void doSomething(){
createObject();
}
private void createObject(){
obj = new Object();
System.out.println("Created MyClass instance");
}
}
Your method should return object1 instead of void and you have to add the following line at the end of the method:
return createdObject;
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.
I have the following class which stores a list of object arrays.
public class Test {
private List<Object[]> list = new ArrayList<Object[]>();
public void addList(Object... obj) {
list.add(obj);
}
public void addList(List<Object> lst) {
list.add(lst.toArray());
}
}
When I call the following, the overloaded method addList(Object... obj) is called but I want the addList(List<Object> lst) to be called. How can I do this?
public class Main {
public static void main(String[] args) {
Test testObj = new Test();
List<String> myStrings = new ArrayList<String>();
myStrings.add("string 1");
myStrings.add("string 2");
myStrings.add("string 3");
// The variable argument method is called but this is a list!
testObj.addList(myStrings);
}
}
Change List<Object> to List<?> to capture lists of any type of object. I tried this and it printed "in List":
import java.util.ArrayList;
import java.util.List;
public class Test {
private List<Object[]> list = new ArrayList<Object[]>();
public void addList(Object... obj) {
System.out.println("in object");
list.add(obj);
}
public void addList(List<?> lst) {
System.out.println("in List<?>");
list.add(lst.toArray());
}
public static void main(String[] args) {
Test testObj = new Test();
List<String> myStrings = new ArrayList<String>();
myStrings.add("string 1");
myStrings.add("string 2");
myStrings.add("string 3");
// The variable argument method is called but this is a list!
testObj.addList(myStrings);
}
}
It's problem of Java Generic. You cannot assign List<String> to List<Object>.
See also: Java Reference assignment with generic lists
Rewrite the type of you non-variadic method to use a wildcard:
public void addList(List<?> lst) {
list.add(lst.toArray());
}
Then List<String> will be a subtype of the parameter type.
List<String> is not a subclass of List<Object>. So that overload will never be called, even if you remove the ... variant.
Change your method to
public void addList(List<?> lst) {
list.add(lst.toArray());
}