• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList Help  RSS feed

 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an ArrayList. What I have done is used another arraylist to add data. That is,

List sheetData = new ArrayList();
List data = new ArrayList();
//using a for loop iterating through an excel sheet (using poi.apache library)
data.add(cell);
//after adding values from the sheet into 'data'
sheetData.add(data);

so my arraylist sheetData looks something like this.
[[Agent, GrossSales, Cancellations, NetSales, GrossRevenue, NetRevenue, Plumbing&Drainage, Gas, Electricity, Boiler, CentralHeating, AvgPremium, null], [Andy, null, null, 18.0, 748.49, 674.54, 21.0, 5.0, 6.0, 8.0, 6.0, F2/D2, null], [Barry, 1.0, 0.0, 1.0, 29.97, 29.97, 1.0, 0.0, 0.0, 0.0, 0.0, 29.97, null]]

so is there a way to access them individually, as in
sheetData-->0-->3 = NetSales
sheetData-->2-->3 = 1.0
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ruban Raj Eugin Francis wrote:
so is there a way to access them individually, as in
sheetData-->0-->3 = NetSales
sheetData-->2-->3 = 1.0


Yes, but you'll make life easier on yourself if you forget the List of Lists and instead define an object that corresponds to one "row" and make a List of those instead.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The List interface provides a get() method that takes an int value representing an index position. Using this method you can directly access a value at a specific position, just like you would using an array.
So, give that a try?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

So, sheetData is a List that contains Lists. It would be better when you'd use generics; you'd have better type-safety, you wouldn't need to use casts when you get something out of the map, and it makes it clear what kind of data the lists contain.

You could get data like this.

Example with generics:

 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeff, Thanks for the reply. But I am not that great with Java. Could you please guide me in doing that? I mean
" define an object that corresponds to one "row" and make a List of those instead. "
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ruban Raj Eugin Francis wrote:Hello Jeff, Thanks for the reply. But I am not that great with Java. Could you please guide me in doing that? I mean
" define an object that corresponds to one "row" and make a List of those instead. "


Each row has an agent, a gross sales, a net sales, and so on, right? That row reprsents one... what? One agent's sales?

A key part of OO programming is defining classes that represent the "real world" objects and ideas we are dealing with. So, if it were me, I'd define a class called AgentSales, or whatever is actually appropriate for your business domain. That class would have fields like agent, grossSales, cancellations, netSales, etc. For each "row" (or each "inner list" in what you curently have), I would create a new AgentSales object, and fill in its fields accordingly. Then I would add it to a List. Or perhpas to a Map key by the agent. It depends on how you're going to use these things.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!