I'm new to Java. I want to create Java Array and insert data into it. I found this code:
ArrayList<SessionsArray> sessionList =
new ArrayList<SessionsArray>(
Arrays.asList( new SessionsArray("A0001", "Intel CPU", new BigDecimal("700.00"), 1),
new SessionsArray("A0002", "Harddisk 10TB", new BigDecimal("500.00"), 2),
new SessionsArray("A0003", "Dell Laptop", new BigDecimal("11600.00"), 8),
new SessionsArray("A0004", "Samsung LCD", new BigDecimal("5200.00"), 3),
new SessionsArray("A0005", "A4Tech Mouse", new BigDecimal("100.00"), 10)
));
How I can declare the Array in tho parts and insert and remove data form it? Something like this:
//declare the Array
ArrayList<SessionsArray> sessionList = new ArrayList<SessionsArray>();
//insert into Array
ArrayList<SessionsArray>(Arrays.asList( new SessionsArray("A0001", "Intel CPU", new BigDecimal("700.00"), 1),
new SessionsArray("A0002", "Harddisk 10TB", new BigDecimal("500.00"), 2),
new SessionsArray("A0003", "Dell Laptop", new BigDecimal("11600.00"), 8),
new SessionsArray("A0004", "Samsung LCD", new BigDecimal("5200.00"), 3),
new SessionsArray("A0005", "A4Tech Mouse", new BigDecimal("100.00"), 10)
));
Best wishes
**EDIT**
I want to use it because I want to get data from database and pass it to JSF page? Is there other way to pass data from database to JSF page?
**EDIT 2**
How I can remove just one element from the list for example only A0002
?
Use addAll()
:
//declare the ArrayList
ArrayList<SessionsArray> sessionList = new ArrayList<SessionsArray>();
//insert into the ArrayList
sessionList.addAll(Arrays.asList(
new SessionsArray("A0001", "Intel CPU", new BigDecimal("700.00"), 1),
new SessionsArray("A0002", "Harddisk 10TB", new BigDecimal("500.00"), 2),
new SessionsArray("A0003", "Dell Laptop", new BigDecimal("11600.00"), 8),
new SessionsArray("A0004", "Samsung LCD", new BigDecimal("5200.00"), 3),
new SessionsArray("A0005", "A4Tech Mouse", new BigDecimal("100.00"), 10)
));
PS As others have pointed out, arrays and ArrayLists
are not the same. What you're using is an ArrayList
. It's a collection class that implements the List
interface and is backed by an internal array.
The example your looking at is quite complex for a basic java array. In fact it does not use an array, it uses an ArrayList which is a datastructure that mimics an array, but provides handy utility functions.
I would recommend reading some basic articles on arrays. Start here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
This is an ArrayList, not an array. ArrayLists are dynamic arrays. If you wish to use an ArrayList, you can simply use the add() method to add information to it. (Or addAll(), as somebody else mentioned.)
If you wish to use an array, you would use different syntax. Do you need it to by dynamic? If not, the overhead may not be worth it. It mostly depends on what you're using it for.
Fixed for 1 by 1 add
//declare the Array
ArrayList<SessionsArray> sessionList = new ArrayList<SessionsArray>();
//insert into Array
sessionList.add( new SessionsArray("A0001", "Intel CPU", new BigDecimal("700.00"), 1));
sessionList.add( new SessionsArray("A0002", "Harddisk 10TB", new BigDecimal("500.00"), 2));
sessionList.add( new SessionsArray("A0003", "Dell Laptop", new BigDecimal("11600.00"), 8));
sessionList.add( new SessionsArray("A0004", "Samsung LCD", new BigDecimal("5200.00"), 3));
sessionList.add( new SessionsArray("A0005", "A4Tech Mouse", new BigDecimal("100.00"), 10));
In answer to your edit 2: how to remove the SessionsArray that contains "A0002":
for(int i = 0; i< sessionList.size(); i++)
{
if(sessionList[i].nameOfFieldWithA0002.equalsIgnoreCase("A0002"))
{
sessionList.remove(i);
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.