I am trying to make a method to breakdown an arrayList of objects into multiple arrayLists based on an attribute value of the myObj class.
private static ArrayList<ArrayList<Ticket>> splitList(ArrayList<Ticket> arrayList){
ArrayList<ArrayList<Ticket>> smallLists = new ArrayList<>();
for(int i = 0; i < arrayList.size(); i++){
for(Ticket eachTick: Ticket.getTickets()){
if(arrayList.get(i).getCategory().equals(eachTick.getCategory())){
smallLists.add(...);
}
}
}
return smallLists;
}
If there is a better way to do what I am attempting, please advise me.
Are you trying to create a list for each ticket category found in the input list? If that's the case, then I agree with Dawood ibn Kareem: use a map that looks up the proper list based on the category. Something like this:
private static ArrayList<ArrayList<Ticket>> splitList(ArrayList<Ticket> inputList) {
Map<String, ArrayList<Ticket>> map = new HashMap<>();
// your ticket categories here
// (array used for simplicity; you should use a Set, and you should also maintain the valid categories inside your Ticket class, not here)
String[] categories = new String[] {'a', 'b', 'c' };
for (int i = 0; i < categories.length; i++) {
map.put(categories[i], new ArrayList<Ticket>());
}
for (int i = 0; i < inputList.size(); i++) {
Ticket t = inputList.get(i);
map.get(t.getCategory()).add(t);
}
// Convert to list of lists
return new ArrayList<>(map.values());
}
Related
I have an arraylist in which
ArrayList<ArrayList<String>> listofItems=new ArrayList<ArrayList<String>>();
in the list of items I have items like this
[[1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][22]]
how to iterate and store in another array these values split into two like
[1,2,3,4,5,6,7,8,9,10,11] and [11,12,13,14,15,16,17,18,19,20,21,22]. I have used advanced for loop
for(ArrayList<String> list:listofItems)
{
for (String s:list)
{
//I dont know how to add logic here.
}
}
I am assuming here that both lists will also hold string type data.If you want Integer type,you can parse while adding.
ArrayList<ArrayList<String>> listofItems = new ArrayList<ArrayList<String>>();
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
for (ArrayList<String> list : listofItems) {
for (String s : list) {
// there shud be some condition how much elements you want in a
// list or some condition
// to decide in which list we need to add item
if (list.size() < 12)
list1.add(s);
else
list2.add(s);
}
}
Use something like that:
if (yourlist.size()>0) {
List<String> first = yourList.subList(0, (int)Math.ceil(yourlist.size()/2.0));
List<String> second = yourList.subList((int)Math.ceil(yourlist.size()/2.0), yourlist.size());
}
Would be separate your list in 2 lists divided in the half.
You can use this code to store your list into another list.
List<String> stringList = new ArrayList<String>();
for(ArrayList<String> list:listofItems) {
for (String s:list) {
stringList.add(s);
}
}
In this case, when you add info to listOfItems, you should try example like this
for(int i = 0; i< 2; i++){
for(int j = 0; j < n; j++){
/// add util break n
lists.get(i).add(strTemp);
}
}
You can change i < 2 into any number you want. I only make this sample for you.
Hi there i have this code
public void comboCountry(List<Countries> cs) {
country = db.fillComboCountries();
DefaultComboBoxModel dcbm = (DefaultComboBoxModel) CountryComboBox.getModel();
for (int i = 0; i < country.size(); i++) {
String row[] = new String[country.size()];
//row[i] = String.valueOf(country.get(i).getCountryId());
row[i] = country.get(i).getCountryName();
// row[i] = country.get(i).getCountryCode();
dcbm.addElement(row);
}
}
with country = db.fillComboCountries(); i query the database and load everything into an ArrayList.
The Arraylist is country.
When i load my data into combobox i get
[Ljava.lang.String;#fdfc58
how can i avoid that and get the value that i want?
I have try with Arrays.tostring(), but i get also [ ].
Instead of dcbm.addElement(row); use dcbm.addElement(country.get(i).getCountryName());
With this, you will add individual elements of an array rather than array itself. Also you would avoid creating arrays with every item in the country list.
You are creating a new String array in each iteration, you should initialize the array before the for loop:
String row[] = new String[country.size()];
for (int i = 0; i < country.size(); i++) {
...
}
If you only want to add the countryName in each iteration, you don't need the array at all:
for (int i = 0; i < country.size(); i++) {
String countryName = country.get(i).getCountryName();
dcbm.addElement(countryName);
}
Or:
for (Country c : country) {
dcbm.addElement(c.getCountryName());
}
I have a list of items that need to remove their repeated ones and then copy the list into another list. The problem is that I cant copy the list into the other list.
Code
.....
private List mylist = new ArrayList();
.....
LinkedHashSet hs = new LinkedHashSet();
hs.addAll(mylist);
mylist.clear();
mylist.addAll(hs);
MyClass.getItems().clear();
MyClass.setItems(mylist);
MyClass.java
.....
private List Items = new ArrayList();
public void setItems(List myItems) {
for (int i = 0; i < myItems.size(); i++) { <<This loop shows the items
System.out.println(myItems.get(i));
}
this.Items.clear();
this.Items.addAll(myItems);
for (int i = 0; i < Items.size(); i++) { << this loop does not show anything
System.out.println(Items.get(i));
}
}
Desired result
mylist >> a,b,c,a,d,c
change to a,b,c,d
then copy to items
items >> a,b,c,d
you can use a LinkedHashSet. This will preserve the insertion order and insure no duplicates.
for problem number two are you adding anyting to the list. the code you have works
List<Integer> mylist = new ArrayList<Integer>();
mylist.add(3);
mylist.add(3);
HashSet hs = new HashSet();
hs.addAll(mylist);
mylist.clear();
mylist.addAll(hs);
System.out.println(mylist.size()); //prints 1
System.out.println(hs.size());// prints 1
RESPONSE to edited question:
They both seem to print out the list fine
public class Tmp {
private List<Integer> Items = new ArrayList<Integer>();
public void setItems(List<Integer> myItems) {
for (int i = 0; i < myItems.size(); i++) { //<<This loop shows the items
System.out.println(myItems.get(i));
}
this.Items.clear();
this.Items.addAll(myItems);
System.out.println();
for (int i = 0; i < Items.size(); i++) { //<< this loop also shows the item
System.out.println(Items.get(i));
}
}
public static void main(String[] args) {
Tmp t = new Tmp();
List<Integer> myList = new ArrayList<Integer>();
myList.add(3);
myList.add(4);
t.setItems(myList);
}
}
Are both these pieces of code in the same class and both list variables pointing to the same arrayList instance. if so calling clear on one clears out both lists (since both variables are pointing to the same list)
How to get String from ArrayList if my code like
ArrayList<String> PItoList = new ArrayList();
for (int i = 0; i < AllPunchList.size(); i++) {
PItoList.add(AllPunchList.get(i).toString());
}
I want to split item in ArrayList one by one.
You could do something like this:-
for(String eachString : PItoList){
// eachString.split(regex);
}
Traverse through each element in the List and do whatever you want to do in with each Element!
Try this :-
ArrayList<String> PItoList = new ArrayList<String>();
for (int i = 0; i < AllPunchList.size(); i++) {
PItoList.add((String)AllPunchList.get(i));
}
In the above example I have printed all elements in ArrayList. In this manner you can get all elements.
Then why you need split ?
Hope it will help you.
I am building an array based off comparing two other arrays. But when I initalize my third array I have to set the length. But that is making my array have null objects in some instances. Is there away I can drop the empty/null postions in the array. See my code so far below:
private String[] tags = new String[] { "Mike", "Bob", "Tom", "Greg" };
private boolean[] selected = new boolean[tags.length];
public String[] selected_tags = new String[tags.length];
for (int i = 0; i < tags.length; i++) {
if (selected[i] == true){
selected_tags[i] = tags[i];
}
}
I left out the code for the checkboxes that builds the Boolen selected [].
Either way if I only select 2 tags then my selected_tags[] array will be Mike, Bob, Null, Null
I need to get the Null Null out. Thanks in advance!
You can use ArrayList, instead of array.
private String[] tags = new String[] { "Mike", "Bob", "Tom", "Greg" };
private boolean[] selected = new boolean[tags.length];
public List<String> selected_tags = new ArrayList<String>();
for (int i = 0; i < tags.length; i++) {
if (selected[i] == true){
selected_tags.add(tags[i]);
}
}
No, you can't drop the null values (and change the length of the array) after you've created it. You'll have to create a new one (or for instance use an ArrayList as illustrated below):
List<String> list = new ArrayList<String>();
for (int i = 0; i < tags.length; i++)
if (selected[i] == true)
list.add(tags[i]);
// Convert it to an array if needed:
selected_tags = list.toArray(new String[list.size()]);
As others have mentioned, this is much easier with an ArrayList. You can even get a regular array from it with the toArray function.
Without using ArrayList, you would have to figure out the length first and not include the null values. As you can see, that's a little messy:
int length = 0;
for( boolean b : selected ) if(b) ++length; // Count the "true"s
String[] selected_tags = new String[length];
for( int i = 0, j = 0; i < tags.length; i++ )
if( selected[i] )
selected_tags[j++] = tags[i];
Instead of using a standard Java array, you should use an ArrayList : it'll allow you to add elements to it, automatically growing the list as needed.
Basically, you'd first declare / instanciate the ArrayList, without specifying any kind of size :
public ArrayList<String> selected_tags = new ArrayList<String>();
And, then, in your loop, you'd use the add() method to add items to that ArrayList :
selected_tags.add(tags[i]);