How can I transfer ArrayLists between classes? - java

I am a rather large-scale noob. I have come across the answer a multitude of times, but, it was just not in a format I could understand. Right now, I am attempting to send a List full of values, to a separate class. I tried each of the resources but to no avail. Apologies if this is an inappropriate question here, a simple ask-with-no-code.

You need a method or a constructor on the class the receives the ArrayList that accepts ArrayLists.
Pseudocode:
public class Class1 {
//Constructor
public Class1(){}
//Methods
public ArrayList createArray(){
//Your code here
}
}
public class Class2 {
//Constructor
public Class2(ArrayList myArray){
//Your code here
}
}
public class Test{
Class1 c1 = new Class1;
ArrayList array = c1.createArray();
Class2 c2 = new Class2(array);
}

class list_receiver{ //class in which you want to send your list
public void print_array_list(ArrayList<String> l ){
System.out.println(l);
}
}
class list_sender{//class from where you are sending your list
public static void main(String args[]){
list_receiver r = new list_receiver();
ArrayList<String> list=new ArrayList<String>();
list.add("A");
list.add("B");
r.print_array_list(list);
}
}

Related

Three Classes, one Main, one for ArrayList, and one for print. how to make them work together?

newbie here! Here's the problem. I wanted to make a separate class for custom methods on an ArrayList for food items, and another separate class on a method to use just for printing the food items created in ArrayList, and I wanted to call them in the Main method. here's what I wrote in the "Foods" class:
import java.util.ArrayList;
public class Foods {
ArrayList<String> foods = new ArrayList<String>();
public void addFood(String input) {
foods.add(input);
}
public void setFood(int number, String input) {
foods.set(number, input);
}
public void removeFood(int numebr) {
foods.remove(numebr);
}
public void clearFood() {
foods.clear();
}
public int sizeFood() {
return foods.size();
}
public String getFood(int i) {
return foods.get(i);
}
}
here's the "Print" class:
public class Print {
public void printFoods() {
Foods foods = new Foods();
for (int i=0; i<foods.sizeFood(); i++)
System.out.println(foods.getFood(i));
}
}
and here's Main:
public class Main {
public static void main(String[] args) {
Foods foods = new Foods();
Print print = new Print();
foods.addFood("Beer");
foods.addFood("Tequila");
print.printFoods();
}
}
and the result is Blank!
So to my understanding, I'm making two instances of "Foods" class, one in the Main class and another in Print class, and that's why when I call the print method I get blank result. so I tried to convert "Print" class to Static so It might see the initialization of Foods class in the Main class and therefore I could remove the instance of Foods class created in Print class. but since converting Print class to Static wasn't allowed, I'm stuck!
I would really appreciate it if you guys could suggest a work around this issue. and one thing to mention, moving the print method to the Main class is a no go, cause I really want to keep different functions organized and separated from the main class. thanks
The core problem is that the Print class has its own instance of Foods so it doesn't matter what you add to the instance defined in Main. I would argue that the behavior you have implemented in the Print class should be moved to the Foods class.
public class Foods {
... the other stuff ...
public void printFoods() {
for (String food : foods) {
System.out.println(food);
}
}
}
Then usage becomes...
public class Main {
public static void main(String[] args) {
Foods foods = new Foods();
foods.addFood("Beer");
foods.addFood("Tequila");
foods.printFoods();
}
}
The problem is that you are creating a new object. Try this:
public class Print {
public void printFoods(Foods foods) {
for (int i=0; i<foods.sizeFood(); i++)
System.out.println(foods.getFood(i));
}
}
And
public class Main {
public static void main(String[] args) {
Foods foods = new Foods();
Print print = new Print();
foods.addFood("Beer");
foods.addFood("Tequila");
print.printFoods(foods);
}
}
The issue here is that you always print a new list instead of the one created externally. As #luk2302 said in the comments section, offer the list created externally to the printFoods() method and remove the Foods foods = new Foods(); from inside the method.
As an example:
print.printFoods(foods);
public void printFoods(Foods foods) {
// Foods foods = new Foods(); // remove it
for (int i=0; i<foods.sizeFood(); i++)
System.out.println(foods.getFood(i));
}
Foods in your Print class is different from Foods in main method.
As suggested by #luk2302, you can add a parameter to your printFoods method like this:
public class Print {
public void printFoods(Foods foods) {
for (int i=0; i<foods.sizeFood(); i++)
System.out.println(foods.getFood(i));
}
}
And call the printFoods with the foods as argument:
public class Main {
public static void main(String[] args) {
Foods foods = new Foods();
Print print = new Print();
foods.addFood("Beer");
foods.addFood("Tequila");
// Pass the foods object in printFoods method
print.printFoods(foods);
}
}

Question about the mechanism of the ArrayList

Basically, when I passed arguments in Java, I knew it was passing only value.
However, the following code shows that the add method executed on SubClass's SubMethod affects ArrayList of MainClass.
MainClass.java
public class MainClass{
public satatic void main(String[] args){
List list = new ArrayList<>();
SubClass subClass = new SubClass(list);
subClass.subMethod();
System.out.println(list) // Why added value???
}
}
SubClass.java
public class SubClass{
private List list;
public SubClass(List list){
this.list = list;
}
public void subMethod(){
list.add(1);
list.add(2);
}
}
When I did the same thing with a HashMap's put, there was no effect on the HashMap of the MainClass.
I would like to know why only ArrayList is causing these results and what is happening inside Java.
Update
The code for the hashmap version is as follows:
MainClass.java
public class MainClass{
public satatic void main(String[] args){
Map map = new HashMap<>();
SubClass subClass = new SubClass(map );
subClass.subMethod();
System.out.println(map) // Not putting value
}
}
SubClass.java
public class SubClass{
private Map map;
public SubClass(Map map){
this.map= map;
}
public void subMethod(){
map = someGenerationHashMap(arg1, arg2);
}
}
It's not about ArrayList. Any object you pass as an argument can be modified. What is passed by value is the address of the object, not the object itself.
In the Map version, you are not making any operation that could modify it. In the list version instead, you are making an add.
Make sure not to confuse objects with primitives. For example, make sure not to confuse int with Integer.

How to send ArrayList send an other class [duplicate]

This question already has answers here:
accessing a variable from another class
(7 answers)
Closed 6 years ago.
How can I send an ArrayList from one class to another in java?
I want to send a variable in a form of a object from one class to another. I tried several ways such as final static but I failed.
Do you have any solution?
public class A(){
private int a1,a2;
public geta1(int a1);
public geta2(int a2);
public seta1(int a1);
public seta2(int a2);}
public class class1(){
A =new A();
A.seta1(5);
//....................}
public class class2(){
//}
How I can build an object in class one and a1=5 in a way that this object could be available in class 2 too with this amount.
You could store the ArrayList in class1 and create a getter method that returns it.
e.g.
import java.util.ArrayList;
public class class1 {
private ArrayList<Integer> myList;
public ArrayList<Integer> getList() {
return myList;
}
//..other methods of the class
}
And then:
class1 object = new class1();
ArrayList<Integer> myList = object.getList();
You could also make it static (part of the class, rather than a part of an instance of the class) if that's what you need
import java.util.ArrayList;
public class class1 {
private static ArrayList<Integer> myList;
public static ArrayList<Integer> getList() {
return myList;
}
//..other methods of the class
}
and then just call
ArrayList<Integer> myList = class1.getList();

ArrayList no suitable method

I want to add everything from ArrayList in class Taster in another ArrayList in class Tastatura. It's calling no suitable method error and I don't really know what to do and how to do it . I would be really thankfull if someone quickly explained me how this work and write my code correctly.
This is main class:
public class Tastatura {
public static void main(String[] args) {
Taster slovo = new Taster();
ArrayList<String> collection1 = new ArrayList<String>();
collection1.addAll(slovo); //<--- error here
}
}
another class:
public class Taster {
public Taster(){
ArrayList<String> Slova = new ArrayList<String>();
Slova.add("q");
Slova.add("w"); }}
The problem is by collection1.addAll(slovo); you are adding an object to a collection.
The addAll method requires a Collection as argument.
You class should look like this :
public class Taster {
private ArrayList<String> Slova;
public Taster() {
Slova = new ArrayList<String>();
Slova.add("q");
Slova.add("w");
}
public ArrayList<String> getList() {
return Slova;
}
}
And in you main method :
collection1.addAll(slovo.getList());
OR
You don't need to change the Taster class :
Just change your addAll to :
collection1.addAll(slovo.Slova);
try this :
collection1.addAll(slovo.Slova) ;
and change your Taster class to this :
public class Taster {
public ArrayList<String> Slova = new ArrayList<String>();
public Taster(){
Slova.add("q");
Slova.add("w");
}
}
You are trying to add class Taster into method, which expects String or something like that (ArrayList<String> in this case)
:)
Nafas code should work..
Other way is to write your own "add" method in your Taster class (which takes actual ArrayList and adds in it every item from ArrayList given in param, and returns that result)

android ArrayList is empty after getter method

I have a class that implements getter and setter methods and related code as follows.
ArrayList<String> viewArray = new ArrayList<String>();
public ArrayList<String> getView() {
return viewArray;
}
from my activity, I am trying to get acces to stored array like:
ArrayList<String> al = new ArrayList<String>();
al = parsedExampleDataSet.getView();
But "al" receives no data. However, when getView() is executed, viewArray is filled properly. What am I missing? Thank you.
Others have make some good comments but I thought I'd take you through the code as I see it.
public class SomeClass {
// this is local to this class only
ArrayList<String> viewArray = new ArrayList<String>();
public void process() {
// i'm guessing there is some sort of processing method that is called
}
public ArrayList<String> getView() {
return viewArray;
}
}
Here's your activity class annotated with some details about the value of a1:
public class YourActivity {
ArrayList<String> al = new ArrayList<String>();
public void someMethod() {
// here a1 will be the same blank List you initialized it with
// unless someMethod() has been called before or a1 modified elsewhere
al = parsedExampleDataSet.getView();
// after the call to getView, a1 is now a reference to SomeClass.viewArray
// the ArrayList that a1 was initialized with is then garbage collected
}
}
Please edit your question to explain more what you are having problems with.

Categories