This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Query on arraylist

 
Greenhorn
Posts: 21
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

If I have an arraylist of strings, and the content of this arraylist is essentially as follows:
[divya, divya1, divya2, divyachan, divyachan1] while inserting a new value into the list, e.g. if i want to insert "divya" i need to loop through the list check if it already exists, if yes, i need to see if there is already a sequence that is being followed and based on that i need to append the next number in the sequence and insert it into the list. In our case, the list already contains divya, divya1 and divya2 so now when i try inserting divya again, it should insert it as divya4. if we try inserting divyachan again, it should loop through the list and since divyachan and divyachan1 already exist, insert it as divyachan2 and so on.
I would like help on this. Any help is appreciated...

Thanks,
Divya
 
Sheriff
Posts: 22567
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A list sounds like a wrong structure. It sounds more like you need to store a count for each value; e.g. for "divya" the count currently is 3 ("divya", "divya1", "divya2") and will be increased to 4, and for "divyachan" the count currently is 2 ("divyachan", "divyachan1") and will be increased to 3.

You can do that with a Map<String,Integer>. Insertion is done as follows:
Retrieval, shown using printing of the values:
An alternative that allows you to work in a similar way is to use a Bag from Apache Commons Collections. The retrieval would be the same; the insertion is handled by the Bag.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why use ArrayList? Looks like you would like to store unique values in your list, so it's better to use some Set. Set is 'a collection that contains no duplicate elements'. So you could use its 'add' function as indicator - if it returns true, element is successfully added, if false - such element is already exists and was not added. Also you could use collection's 'contains' method to find out whether element is on the list instead of iterating through it.
 
Greenhorn
Posts: 7
Eclipse IDE Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

try the below code..hope it might help you.


static List<String> list = new ArrayList<String>();

public static void addElement(String str) {



String elements[] = new String[list.size()];
elements = list.toArray(elements);

int tempi = 0;
for (int i = 0; i < elements.length; i++) {
if (elements[i].startsWith(str)) {
tempi++;
}
}

if (tempi > 0) {
list.add(str + tempi);
}
else
list.add(str);

System.out.println(list);

}

Thanks,
Venkat.




 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This dodgy code fails when input itself contains "Dhivya2" (instead of "Dhivya") or that incremented value from initial name... strictly not advised to use array list..



Thanks much!
John

 
Divya Chandrasekhar
Greenhorn
Posts: 21
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the solutions, but it doesn't really resolve my problem...
I should have given you all the details in the first place...
The thing is that I get this list from the DB. My logic works on creating a username and if the username I have created already exists, then i need to increment the last digit appended to the username by 1 and send this value to be inserted into the DB. i.e. if the list contains divya1 then the username I need to create would be divya2 and so on.
Is there a way to achieve this?

I am using the contains method to check if the username I have created exists on the list, however I am unable to figure out how to go about the increment thingy...
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could you please give the input & output you need from your program? - might be better useful for others to help you!

Is this what you want? a Name string... or just count occurrence & return an incremented int value

Dhivya -> Dhivya1 -> Dhivya2 -> Dhivya3

now comes a Dhivya1

Dhivya1 -> Dhivya11 -> Dhivya12 -> Dhivya13
======================================

Say this is in the list now [Dhivya, Dhivya1, Dhivya2,Dhivya3]

1. Say what should happen if a "Dhivya" comes?

2. What should be returned if a "Dhivya2" comes?
 
Divya Chandrasekhar
Greenhorn
Posts: 21
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:Hi,

Could you please give the input & output you need from your program? - might be better useful for others to help you!

Is this what you want? a Name string... or just count occurrence & return an incremented int value

Dhivya -> Dhivya1 -> Dhivya2 -> Dhivya3

now comes a Dhivya1

Dhivya1 -> Dhivya11 -> Dhivya12 -> Dhivya13
======================================

Say this is in the list now [Dhivya, Dhivya1, Dhivya2,Dhivya3]

1. Say what should happen if a "Dhivya" comes?

2. What should be returned if a "Dhivya2" comes?



Hey John,
Initially the list that is coming from the DB contains:
Dhivya -> Dhivya1 -> Dhivya2 -> Dhivya3
Now according to my logic I have created the username Dhivya based on some Business Logic, then i need to check if this exists in the list (this has been handled)
if the username that we have created in the step above exists in the list from DB,
the list should be iterated to check how many names like 'Dhivya_' exist in the list and the username should be modified to Dhivya4 since we already have upto Dhivya3 in the list. (This needs to be implemented)
this modified username is then sent to DB (this part has been handled)

Hope this makes things clearer...
 
venkat prathap
Greenhorn
Posts: 7
Eclipse IDE Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i would suggest stored procedure, i think ..... using procedure it will be solved easily
 
Divya Chandrasekhar
Greenhorn
Posts: 21
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Venkat,

I am not allowed to use a stored proc.

Thanks,
Divya
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dhivya,

i am poor in understanding requirements sorry if i got it wrong again... hopefully someone helps you better...

however my piece of code below

 
Rob Spoor
Sheriff
Posts: 22567
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That can be done a lot cleaner. Less efficient, but cleaner:
The logic is pretty easy: if the list does not contain the name (equivalent to isNamePresent being false) you return 0. Otherwise you start at 1, checking all the names: Dhivya1, Dhivya2, Dhivya3, etc, until you find the one that's not in the list. You return that number.

Now I said this code is less efficient. That's because it uses a nested loop: the outer loop with the while, and an inner loop for contains. You can make it more efficient by using a Set (most likely HashSet or LinkedHashSet, unless you want case insensitive in which case TreeSet is better) instead of a List, but that would probably requite a change of quite a bit of code.
 
Divya Chandrasekhar
Greenhorn
Posts: 21
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, I think this looks neat and is a lot less complex than what John suggested and what I feared
Thanks for your inputs Rob and John!
 
Rob Spoor
Sheriff
Posts: 22567
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good you got the solution. And kudos to Rob for giving simpler code.

 
Divya Chandrasekhar
Greenhorn
Posts: 21
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rob,

I modified this slightly to suit my need and my code works beautifully..
Thanks once again!

Divya
 
reply
    Bookmark Topic Watch Topic
  • New Topic