How do i group more ArrayLists in one variable? - java

I have this constructor in another class
public <T> initializingWorkbook( Sheet sheet,List <T> column, int index)
{
int rows = sheet.getRows();
for(int row = 1;row < rows;row++)
{
String i = sheet.getCell(index, row).getContents();
coloana.add(Double.parseDouble(i)) ;
}
}
In the main() function I have to initialize all those Arrays at once. So when I call the initializingWorkbook() function I want all these arrays to be filled with data (I'm taking that data from an excel file).
ArrayList <Long> timeColumn = new ArrayList<>();
ArrayList <Double> pc1Column = new ArrayList<>();
ArrayList <Double> pc2Column = new ArrayList<>();
ArrayList <Double> ph1Column = new ArrayList<>();
ArrayList <Double> ph2Column = new ArrayList<>();
ArrayList <Double> ph3Column= new ArrayList<>();
Edit: I added what this constructor contains. So basically
-Sheet sheet is the sheet from where I read data;
-List <T> column is the variable where I stock the column;
-index is the number of the column.
I need to load all data in my ArrayList variables at once.
Thanks for your answers.

I think you are trying to represent a table, aren't you?
You could create a class for one record in the table, and store an ArrayList of this class.
public class MyClass {
public Long timeColumn;
public Double pc1Column;
public Double pc2Column;
public Double ph1Column;
public Double ph2Column;
public Double ph3Column;
}
You create the ArrayList like this:
ArrayList<MyClass> myTable = new ArrayList<>();
You add an element like this:
for(every row in the table) {
String cell1 = sheet.getCell(0, row).getContents();
String cell2 = sheet.getCell(1, row).getContents();
String cell3 = sheet.getCell(2, row).getContents();
String cell4 = sheet.getCell(3, row).getContents();
String cell5 = sheet.getCell(4, row).getContents();
String cell6 = sheet.getCell(5, row).getContents();
MyClass record = new MyClass();
record.timeColumn = Long.parseLong(cell1);
record.pc1Column = Double.parseDouble(cell2);
record.pc2Column = Double.parseDouble(cell3);
record.ph1Column = Double.parseDouble(cell4);
record.ph2Column = Double.parseDouble(cell5);
record.ph3Column = Double.parseDouble(cell6);
myTable.add(record);
}

The simplest solution would be a List of lists, then access them by index. Another option would be to put them in a Map. The choice would be dependent on how you plan to load the data and use it later.
Note, there is no need to use a concrete type, the generic one would be enough and it is also preferred.
List<Object> listOfObj = new ArrayList<>();

Related

Data structure like hashmap but with the ability to swap indexes in Java

I want to use a data structure to store column names of a JTable with their respective indexes, so that I can select the corresponding data column. If I filter out a column name, then the name and the index gets removed, if I put it back, the index will be the same. If I want to swap two columns, the indexes change but the column names stay the same. It also has to be serializable. Can I do this with a HashMap<String,Integer>?
I managed to load it with values, I basically filtered the dataList with the selected columnNames. I'm not too familiar with maps. As far as a know, I can't "swap" key's in HashMap
private List<Object[]> data = new ArrayList<Object[]>();
private List<String> columnNames = new ArrayList<String>();
public CustomTableModel(List<Object[]> dataList, Map<Integer,String> columnNames) {
for (Map.Entry<Integer, String> set : columnNames.entrySet()) {
this.columnNames.add(set.getValue());
}
ArrayList<Integer> selectedColIndexes = new ArrayList<Integer>();
for (Map.Entry<Integer, String> set : columnNames.entrySet()) {
selectedColIndexes.add(set.getKey());
}
for(int n=0; n<dataList.size(); n++) {
Object[] selectedRow = new Object[columnNames.size()];
for(int j=0; j<selectedColIndexes.size(); j++) {
int index = selectedColIndexes.get(j);
selectedRow[j]=dataList.get(n)[index];
}
data.add(selectedRow);
}
}

java DataBase class

I am trying to make some DataBase class. I know that I want each column to be represented as ArrayList.
I want to create Constructor as that
DataBase(String[] columnNames, String[] types)
so for example
DataBase({"Column1","Column2","Column3"},{"int","string","myOwnType"})
I tried to make it with HashMap which will have column name as key and ArrayList as value.
This is what I got so far.
public class DataFrame {
public HashMap<String, ArrayList> data;
public String[] names;
public String[] types;
public DataFrame(String[] names, String[] types) {
data = new HashMap<String, ArrayList>();
nazwy = new String[_names.length];
typy = new String[_types.length];
for(int i = 0; i < _names.length; i++) {
ArrayList column = new ArrayList<>();
data.put(_nazwy[i], column);
}
}
}
How can I create a column of type given in program? I thought about creating ArrayList and then make Map which will have label and that list, but I cant find how to do that.
In your method, this Loop is re initializing the arraylist so it is not possible to store the records.
for(int i = 0; i < _names.length; i++) {
ArrayList column = new ArrayList<>();
data.put(_nazwy[i], column);
}
Always good to have List. Map will represent each records with column names so that it will easy for processing.
ArrayList list = new ArrayList(50);
HashMap row = new HashMap(columns);
for(int i=1; i<=columns; ++i){
row.put(getColumnName(i),data(i));
}
list.add(row);

Access Values from an ArrayList That is inside another ArrayList

java.util.List records = new java.util.ArrayList();
java.sql.ResultSet rs = selectestatement.executeQuery(query1);
while (rs.next()) {
java.util.List record = new java.util.ArrayList();
record.add(rs.getString("WHLO").trim());
record.add("888509018579");
record.add(rs.getString("ITEM_CODE").trim());
record.add(rs.getString("ARRIVAL_DATE").trim());
record.add(rs.getString("PAIRS_PER_CASE").trim());
record.add(rs.getString("ATS").trim());
records.add(record);
}
In this code, Final arraylist is the "records array". This records arraylist contents few record arrays.
How can i access the 1st element of record arraylist from the records arraylist?
Don't use raw types:
List<List<String>> records = new ArrayList<>();
List<String> record = new ArrayList<>();
...
records.add(record);
This way records.get(i) will return a List<String> instead of an Object, so you can access the elements of the inner List:
String first = records.get(0).get(0);
What you really want is a class containing your row data.
class RecordData {
public String whlo;
public long someNumber = 888509018579;
public String itemCode;
public String arrivalDate;
public String pairsPerCase;
public String ats;
}
and then do
java.util.List<RecordData> records = new java.util.ArrayList<>();
while (rs.next()) {
RecordData record = new RecordData();
record.whlo = rs.getString("WHLO").trim();
record.itemCode = rs.getString("ITEM_CODE").trim();
record.arrivalDate = rs.getString("ARRIVAL_DATE").trim();
record.pairsPerCase = rs.getString("PAIRS_PER_CASE").trim();
record.ats = rs.getString("ATS").trim();
records.add(record);
}
In fact, you want to make the members private and accessible via getters and setters, and use LocalDate for the arrivalDate and int for the pairsPerCase member, but the first point is not using a List to store the retrieved values but wrap it in a business-oriented class.
You can do something like this
((ArrayList)records.get(0)).get(0) to access the first element of the array list that is in the first position of the records array list.
Please note that if you specify what does the records contains (in this case records will contains array lists) then you won't need to cast the element to array list.
List<List<String>> records = new ArrayList<ArrayList>();
{...}
records.get(0).get(0); //You don't need the cast because Java already knows that what it is inside records are Lists

How to get String[] from ArrayList<String[]>, I want to actually get String at index 0 from ArrayList<String[]>?

I have a Class
public class DataRatesString {
private String[] ab1 = {"Auto moto sdhv davjdn adadk", "Rs. 355"} ;
private String[] ab2 = {"sjg atoiu ", "Rs. 200"} ;
private String[] ab3 = {"go to UTOP atup auto", "Rs. 3279"} ;
private String[] ab4 = {"Hid to putho", "Rs. 2424"} ;
private String[] abo5 = {"pithoo to bittu", "Rs. 8457"} ;
private ArrayList<String[]> abCollection = new ArrayList<>();
public void setAbCollection() {
abCollection.add(ab1);
abCollection.add(ab2);
abCollection.add(ab3);
abCollection.add(ab4);
abCollection.add(ab5);
}
public ArrayList<String[]> getAbCollection(){
return abCollection;
}
}
I have another class from which I am calling the list
DataRatesString dataRatesString = new DataRatesString();
dataRatesString.setAbCollection();
ArrayList<String[]> ratesarray = dataRatesString.getAbCollection();
Now I want to get all the strings at position 0 of the Arraylist String[]
I have created a method in the same class but since I am new to java I am unable to figure out exactly how to do it.
This is the method that I tried to make
private List<String> getRatesItemNamelist(){
String[] arraylist;
ArrayList<String> list = new ArrayList<>();
arraylist = new String[ratesarray.size()];
for (int i = 0; i < ratesarray.size(); i++) {
arraylist = ratesarray.get(i);
}
list.add(0, arraylist[arraylist.length]);
return list;
}
This is not working.
Try this:
private List<String> getRatesItemNamelist(){
List<String> list = new ArrayList<>();
for (String[] rates : ratesarray) {
list.add(rates[0]);
}
return list;
}
Here in the for loop, rates[0] will get the first String from the String[] array for each element in ratesarray ArrayList.
So the getRatesItemNamelist() function will return a list of String at index 0 from ArrayList ratesarray.
If i understood properly, you want all arraylist data at position 0. but i don't think you can achieve. even you want you have to add string in multiple arraylist and each string add in at 0 position respectively.
why not try to make code simpler something like this
List<String> data = new ArrayList();
data.add("demo1");
data.add("demo2");
.......
Fetching data at 0 position, you could do this
data.get(0);
Fetch all data, you could use for each loop
for(String s: data){
// here is you each position's data as string
}

Adding two arrays to two columns in JavaFX TableView

I have successfully added one array to one column in TableView with the following code:
data = FXCollections.observableArrayList();
String[] a = {"a", "b"};
yearColumn.setCellValueFactory(new PropertyValueFactory<test3, String>("Year"));
interestColumn.setCellValueFactory(new PropertyValueFactory<test3, String>("Interest"));
principalColumn.setCellValueFactory(new PropertyValueFactory<test3, String>("Principal"));
balanceColumn.setCellValueFactory(new PropertyValueFactory<test3, String>("Balance"));
paymentsAnnual.setItems(data);
for (String anA : a) {
test3 test31 = new test3();
test31.year.setValue(anA);
data.add(test31);
}
test3.java
import javafx.beans.property.SimpleStringProperty;
public class test3 {
public SimpleStringProperty year = new SimpleStringProperty();
public SimpleStringProperty interest = new SimpleStringProperty();
public SimpleStringProperty principal = new SimpleStringProperty();
public SimpleStringProperty balance = new SimpleStringProperty();
public String getYear() {
return year.get();
}
public String getInterest() {
return interest.get();
}
public String getPrincipal() {
return principal.get();
}
public String getBalance() {
return balance.get();
}
}
Now I have a problem when I try to insert a new array to a different column at a time. say I have an array String[] b = {"hello", "hi"}, then I add it using the same for each loop, something like this
for (String anA : a) {
test3 test31 = new test3();
test31.year.setValue(anA);
data.add(test31);
}
for (String anA1 : b) {
test3 test31 = new test3();
test31.balance.setValue(anA1);
data.add(test31);
}
this adds the array but I get an output something like this
Can anyone tell me how to add them together?
UPDATE
How should I add array a and b together, which would give an output like this
UPDATE-2
I do have one way of doing it , by making it a double array
String[] a = {{"a", "b"},{"hello","hi"}};
for (int i = 1; i < a[0].length; i++){
test3 test31 = new test3();
test31.year.setValue(a[0][i]);
test31.balance.setValue(a[1][i]);
data.add(test31);
}
this way I am able to add the contents at a time in its correct row, what if its a single array?
Every item in a TableView's items list represents one row.
Your first loop adds two rows, which each have a year defined but have no other properties defined.
Your second loop adds two more rows, which each have a balance defined but have no other properties defined.
You must modify the existing objects in your List instead of adding more rows.

Categories

Resources