How to use arraylist from one class into another class + add data in Java
How to use arraylist from one class into another class + add data in Java
(OP)
Hello everyone,
I am very new to Java
what I need to do is like below:
Public Class1
that is an arrayclass that holds multi arrays + lists is my thought.
Public Class2
here is where I fill the arrayclass from Class1 with arraydata.
Public Class3
here is where I use the array class1 that reiceved the data in class2
How do I make this?
basically passing arraylist with data to another class
Thank you in advance
Regards
I am very new to Java
what I need to do is like below:
Public Class1
that is an arrayclass that holds multi arrays + lists is my thought.
Public Class2
here is where I fill the arrayclass from Class1 with arraydata.
Public Class3
here is where I use the array class1 that reiceved the data in class2
How do I make this?
basically passing arraylist with data to another class
Thank you in advance
Regards
RE: How to use arraylist from one class into another class + add data in Java
It's not very complicated.
For example if you have a data-class (which contains data + getters and setters) - like this:
CODE
public class RecordSet { ... }
then you can create second class which has method for filling the data.
This method can return the data-class instance - like this:
CODE
public class RecordSetFill { public RecordSet fillInData() { RecordSet result = new RecordSet(); ... return result; } }
then you can create third class which contains method for processing the data - e.g. printing the data.
This method could have the data-class instance as argument - for example:
CODE
public class RecordSetProcess { public void printRecordSet(RecordSet rs) { ... while (rs.next()) { ... System.out.printf(...); ... } } }
Then to test it all together create a fourth class which could look for example like this:
CODE
If you would be interested in other implementation details I could post it here.
RE: How to use arraylist from one class into another class + add data in Java
CODE --> Java
then another class how do I add a new arrayelement here? (CLASS3 to the current ArrayList)
CODE --> Java
how do I loop the array items I added in Class3 in Class4?
CODE --> Java
Thank you in advance
RE: How to use arraylist from one class into another class + add data in Java
IMO the best would be, when I post you my little working example, you take a look at it, try to understand it and then have questions. I didn't use naming like Class1, Class2, ...etc, because after a while I don't know what for is which class
#1. I created the data-class. It holds data in an ArrayList of Records.
1a) So first I created the class for one array element Record. It contains a data structure to hold my financial expenses (amount, date and a note which tells me what for I payed). Getters/setters were generated using the IDE (I have eclipse)
1b) Then I created the class RecordSet which contains an the ArrayList of Record and some methods to work with - which will be used later.
RecorSet.java
CODE
#2. I wrote the class RecordSetFill which fills the data into the RecordSet object. It has only one method fillInData(). This method creates the instance of RecordSet, then adds three Records into RecordSet using the method RecordSet.add() and at end returns the RecordSet object filled with data.
RecordSetFill.java
CODE
#3. Then I created the class RecordSetProcess which processes the RecordSet, i.e. it steps through all Records and prints them. For this purpose I have in the RecordSet the method next(). This method goes to the next Record (if possible) and fetch it's data into internal variable currentRecord. Then using the getters getAmount(), getDatum() and getNote() I will get the data out from data-object.
RecordSetProcess.java
CODE
#4. Finally to test all 3 classes together I created the class TestRecordSetProcessing. In the main() method it first creates the instance of RecordSetFill and calls it's method fillInData() which creates the instance RecordSet filled with data. Then it creates the instance of RecordSetProcess class and calls it's method printRecordSet, which takes as argument the instance of RecordSet and prints all records it contains.
TestRecordSetProcessing.java
CODE
When I run the test class I get this output:
CODE
RE: How to use arraylist from one class into another class + add data in Java
As I looked at your previous question about ArrayList, then maybe what I posted is too much as a first example on this topic. So better take it as second. Here is the modification simplified as possible - this should you try first:
#1. the data-class which holds only ArrayList of Strings:
StringList.java
CODE
#2. This class contains method which creates an instance of data-class, fills it with data and returns it:
StringListFill.java
CODE
#3. This class contains method which takes the data-class as an argument and prints all its elements:
StringListProcess.java
CODE
#4. Finally this is the test class:
TestStringList.java
CODE
Output:
CODE
RE: How to use arraylist from one class into another class + add data in Java
I am current is writing and translate your code. may take a hour lol :)
I am very sure I going to have questions to you later when I am done!
Thank you so much very grateful!
Best regards
Xsi