Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use arraylist from one class into another class + add data in Java 1

Status
Not open for further replies.

Xsi

Programmer
May 29, 2015
121
0
0
SE
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
 
Hi Xsi,

It's not very complicated.

For example if you have a data-class (which contains data + getters and setters) - like this:
Code:
public class [highlight #FCE94F]RecordSet[/highlight] {
  ...
}

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 [highlight #FCE94F]RecordSet[/highlight] 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([highlight #FCE94F]RecordSet[/highlight] 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:
public class TestRecordSetProcessing {
  public static void main(String[] args) {
    // create instance of a class which populates RecordSet
    RecordSetFill recordSetFill = new RecordSetFill();
		
    // call method which returns instance of RecordSet
    RecordSet rs = recordSetFill.fillInData();
		
    // creates instance of a class which processes the recordSet  
    RecordSetProcess recordSetProcess = new RecordSetProcess();
		
    // call method which prints data contained in the RecordSet instance
    recordSetProcess.printRecordSet(rs);
  }
}

If you would be interested in other implementation details I could post it here.
 
In my Case now I have got

Java:
import java.util.ArrayList;

public class Class1 {
	
	public  void MyArr() {
	
	 ArrayList<String> obj = new ArrayList<String>();
	 obj.add("MyTest1");
	 obj.add("MyTest2");
	 obj.add("MyTest3"); 
	 System.out.println("This is"+ obj);
	}
    
  
}

then another class how do I add a new arrayelement here? (CLASS3 to the current ArrayList)
Java:
public class Class3 {
  public static void main(String[] args) {
	  
	  Class1 c1=new Class1();
	  
	  c1.MyArr();

  }
}

how do I loop the array items I added in Class3 in Class4?

Java:
public class Class4 {
  public static void main(String[] args) {
	  
	  Class1 c1=new Class1();
	  
	  c1.MyArr();
  
  }
}


Thank you in advance
 
Hi Xsi,

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:
[COLOR=#800080]import[/color] java.util.ArrayList;

[COLOR=#2e8b57][b]final[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] Record {
    [COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]double[/b][/color] amount;
    [COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] datum;
    [COLOR=#2e8b57][b]private[/b][/color] String note;

    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]double[/b][/color] getAmount() {
        [COLOR=#a52a2a][b]return[/b][/color] amount;
    }

    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] setAmount([COLOR=#2e8b57][b]double[/b][/color] amount) {
        [COLOR=#2e8b57][b]this[/b][/color].amount = amount;
    }

    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] getDatum() {
        [COLOR=#a52a2a][b]return[/b][/color] datum;
    }

    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] setDatum([COLOR=#2e8b57][b]int[/b][/color] datum) {
        [COLOR=#2e8b57][b]this[/b][/color].datum = datum;
    }

    [COLOR=#2e8b57][b]public[/b][/color] String getNote() {
        [COLOR=#a52a2a][b]return[/b][/color] note;
    }

    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] setNote(String note) {
        [COLOR=#2e8b57][b]this[/b][/color].note = note;
    }
}

[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] RecordSet {
    [COLOR=#2e8b57][b]private[/b][/color] ArrayList<Record> recSet = [COLOR=#a52a2a][b]new[/b][/color] ArrayList<Record>();
    [COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] recSetSize = [COLOR=#ff00ff]0[/color];
    [COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] j = [COLOR=#ff00ff]0[/color];
    [COLOR=#0000ff]// data from record on which the cursor position shows[/color]
    [COLOR=#2e8b57][b]private[/b][/color] Record currentRecord = [COLOR=#a52a2a][b]new[/b][/color] Record();

    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] add([COLOR=#2e8b57][b]double[/b][/color] amount, [COLOR=#2e8b57][b]int[/b][/color] datum, String note) {
        Record rec = [COLOR=#a52a2a][b]new[/b][/color] Record();
        rec.setAmount(amount);
        rec.setDatum(datum);
        rec.setNote(note);
        [COLOR=#2e8b57][b]this[/b][/color].recSet.add(rec);
    }

    [COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]boolean[/b][/color] hasNext() {
        [COLOR=#2e8b57][b]this[/b][/color].recSetSize = recSet.size();
        [COLOR=#a52a2a][b]return[/b][/color] (j < [COLOR=#2e8b57][b]this[/b][/color].recSetSize);
    }

    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]boolean[/b][/color] next() {
        [COLOR=#2e8b57][b]boolean[/b][/color] nextElementExist = hasNext();
        [COLOR=#a52a2a][b]if[/b][/color] (nextElementExist) {
            [COLOR=#0000ff]// fetch data in record[/color]
            [COLOR=#2e8b57][b]this[/b][/color].currentRecord = [COLOR=#2e8b57][b]this[/b][/color].recSet.get(j++);
        }
        [COLOR=#a52a2a][b]return[/b][/color] nextElementExist;
    }

    [COLOR=#0000ff]// Getters for getting data from currentRecord[/color]
    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]double[/b][/color] getAmount() {
        [COLOR=#a52a2a][b]return[/b][/color] currentRecord.getAmount();
    }

    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] getDatum() {
        [COLOR=#a52a2a][b]return[/b][/color] currentRecord.getDatum();
    }

    [COLOR=#2e8b57][b]public[/b][/color] String getNote() {
        [COLOR=#a52a2a][b]return[/b][/color] currentRecord.getNote();
    }
}


#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:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] RecordSetFill {
    [COLOR=#2e8b57][b]public[/b][/color] RecordSet fillInData() {
        RecordSet result = [COLOR=#a52a2a][b]new[/b][/color] RecordSet();
        [COLOR=#2e8b57][b]int[/b][/color] n = [COLOR=#ff00ff]0[/color];
        System.out.println([COLOR=#ff00ff]"Start filling records:"[/color]);

        System.out.printf([COLOR=#ff00ff]"    record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
        result.add([COLOR=#ff00ff]44.02[/color], [COLOR=#ff00ff]20180409[/color], [COLOR=#ff00ff]"Payment at the gas station"[/color]);

        System.out.printf([COLOR=#ff00ff]"    record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
        result.add([COLOR=#ff00ff]7.90[/color], [COLOR=#ff00ff]20180413[/color], [COLOR=#ff00ff]"10 Pilsner Beers"[/color]);

        System.out.printf([COLOR=#ff00ff]"    record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
        result.add([COLOR=#ff00ff]92.50[/color], [COLOR=#ff00ff]20180414[/color], [COLOR=#ff00ff]"Saturday shopping with my wife"[/color]);

        System.out.println([COLOR=#ff00ff]"Done.[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]);
        [COLOR=#a52a2a][b]return[/b][/color] result;
    }
}


#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:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] RecordSetProcess {
    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] printRecordSet(RecordSet rs) {
        [COLOR=#2e8b57][b]int[/b][/color] n = [COLOR=#ff00ff]0[/color];
        [COLOR=#2e8b57][b]double[/b][/color] amount;
        [COLOR=#2e8b57][b]double[/b][/color] total = [COLOR=#ff00ff]0[/color];
        System.out.println([COLOR=#ff00ff]"Now processing records:"[/color]);
        [COLOR=#a52a2a][b]while[/b][/color] (rs.next()) {
            n++;
            amount = rs.getAmount();
            System.out.printf([COLOR=#ff00ff]"#%02d: cost=%5.2f, date: %8d, what: %s[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], n,
                    amount, rs.getDatum(), rs.getNote());
            [COLOR=#0000ff]// total cost[/color]
            total += amount;
        }
        System.out.printf([COLOR=#ff00ff]"%d records processed, total expenses = %6.2f [/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], n,
                total);
    }
}


#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:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] TestRecordSetProcessing {
    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String[] args) {
        [COLOR=#0000ff]// create instance of a class which populates RecordSet[/color]
        RecordSetFill recordSetFill = [COLOR=#a52a2a][b]new[/b][/color] RecordSetFill();

        [COLOR=#0000ff]// call method which returns instance of RecordSet[/color]
        RecordSet rs = recordSetFill.fillInData();

        [COLOR=#0000ff]// creates instance of a class which processes the recordSet[/color]
        RecordSetProcess recordSetProcess = [COLOR=#a52a2a][b]new[/b][/color] RecordSetProcess();

        [COLOR=#0000ff]// call method which prints data contained in the RecordSet instance[/color]
        recordSetProcess.printRecordSet(rs);
    }
}

When I run the test class I get this output:
Code:
Start filling records:
    record #01
    record #02
    record #03
Done.

Now processing records:
#01: cost=44.02, date: 20180409, what: Payment at the gas station
#02: cost= 7.90, date: 20180413, what: 10 Pilsner Beers
#03: cost=92.50, date: 20180414, what: Saturday shopping with my wife
3 records processed, total expenses = 144.42
 
Hi Xsi,

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:
[COLOR=#800080]import[/color] java.util.ArrayList;

[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] StringList {
    [COLOR=#2e8b57][b]public[/b][/color] ArrayList<String> strings = [COLOR=#a52a2a][b]new[/b][/color] ArrayList<String>();
}


#2. This class contains method which creates an instance of data-class, fills it with data and returns it:
StringListFill.java
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] StringListFill {
    [COLOR=#2e8b57][b]public[/b][/color] StringList fillData() {
        StringList result = [COLOR=#a52a2a][b]new[/b][/color] StringList();
        [COLOR=#2e8b57][b]int[/b][/color] n = [COLOR=#ff00ff]0[/color];
        System.out.println([COLOR=#ff00ff]"Start filling data:"[/color]);

        result.strings.add([COLOR=#ff00ff]"foo"[/color]);
        System.out.printf([COLOR=#ff00ff]"    record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
        result.strings.add([COLOR=#ff00ff]"bar"[/color]);
        System.out.printf([COLOR=#ff00ff]"    record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
        result.strings.add([COLOR=#ff00ff]"baz"[/color]);
        System.out.printf([COLOR=#ff00ff]"    record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
        result.strings.add([COLOR=#ff00ff]"FooBar"[/color]);
        System.out.printf([COLOR=#ff00ff]"    record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
        result.strings.add([COLOR=#ff00ff]"Spam"[/color]);
        System.out.printf([COLOR=#ff00ff]"    record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
        result.strings.add([COLOR=#ff00ff]"Eggs"[/color]);
        System.out.printf([COLOR=#ff00ff]"    record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);

        System.out.println([COLOR=#ff00ff]"Done.[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]);
        [COLOR=#a52a2a][b]return[/b][/color] result;
    }
}


#3. This class contains method which takes the data-class as an argument and prints all its elements:
StringListProcess.java
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] StringListProcess {
    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] printStringList(StringList stringList) {
        [COLOR=#2e8b57][b]int[/b][/color] n = [COLOR=#ff00ff]0[/color];
        System.out.println([COLOR=#ff00ff]"Now processing List of Strings:"[/color]);
        [COLOR=#a52a2a][b]for[/b][/color] (String string : stringList.strings) {
            n++;
            System.out.println(string);
        }
        System.out.printf([COLOR=#ff00ff]"%d records processed."[/color], n);
    }
}


#4. Finally this is the test class:
TestStringList.java
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] TestStringList {
    [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String[] args) {
        [COLOR=#0000ff]// create instance of a class which populates StringList[/color]
        StringListFill stringListFill = [COLOR=#a52a2a][b]new[/b][/color] StringListFill();

        [COLOR=#0000ff]// call method which returns instance of StringList[/color]
        StringList stringList = stringListFill.fillData();

        [COLOR=#0000ff]// creates instance of a class which processes the StringList[/color]
        StringListProcess stringListProcess = [COLOR=#a52a2a][b]new[/b][/color] StringListProcess();

        [COLOR=#0000ff]// call method which prints data contained in the StringList instance[/color]
        stringListProcess.printStringList(stringList);
    }
}

Output:
Code:
Start filling data:
    record #01
    record #02
    record #03
    record #04
    record #05
    record #06
Done.

Now processing List of Strings:
foo
bar
baz
FooBar
Spam
Eggs
6 records processed.
 
Thank you so much mikrom!

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top