I have a foreach that is suppose to go through and arraylist and perform an action every time an item changes.
So it for something like:
ID | Request
1 | z
2 | e
it sends an email to 1 saying "You have 1 request". Then an email to 2 "You have 1 request" and so on.
My loop doesn't address the last item when there's only 2 items in the list and I'm having a difficult time trying to figure out the elegant way to address it.
Integer managerId = null;
Integer previousManagerId = null;
if(requests != null && requests.size() > 0){
for(Request request : requests){
managerId = request.getId();
if((!managerId.equals(previousId) && previousId != null)){
e.sendEmail(previousId, numReq.toString());
numReq = 0;
}
numReq++;
previousId = managerId;
}
//Suppose to address the last item. Fails when size == 2
if((!managerId.equals(previousId) && previousId != null)){
eusendEmail(previousId, numReq.toString());
}
The last statement of the loop ensures that previousId is always equal to managerId after the loop ends, so it can never go into the if.
I think you always want to send the last email because you know there was at least one request.
You have to save the previousId outside the for each because it will delet this value every time it goes trough the next object ;)
But why are you doing the first if? Wouldn't the foreach do that anyway?
How if you try like this:
String managerId = "";
for(Request request : requests){
managerId = request.getId();
if(!managerId.equals(previousId)){
request.sendEmail(previousId, numReq.toString());
numReq = 0;
}
numReq++;
}
Related
i have a String array with information like this:
name street streetnumber City house flat
jetsons jetstreet 12 london yes no
jetsons jetstreet 10 washingston n y
jetsons jetstreet 10 washingston n y
jetsons jetstreet 10 washingston yes no
ALF alfStreet 3 Shanghai y y
...and so on
now the exercise is to create an new list with unique data which is analyzed.
livingDataArray
analyzedDataList
while(livingDataArray=reader.readLine() != null){
street = livingDataArray[1];
streetNumber = livinDataArray[2];
city = livingDataArray[3;]
if(analyzedDataList.isEmpty()) {
createNewEntry in analyzedDataList(); // that line is fine. ;)
} else {
int analyzedDataSize = analyzedData.size();
for (int i = 0; i <= analyzedDataSize; i++){
if(analyzedData.get(i)[1] == street &&
analyzedData.get(i)[2] == streetNumber &&
analyzedData.get(i)[3] == city ) {
categorize(); // this line is fine also
addToAnalyzedData();
break;
} else if (!(analyzedData.get(i)[1] == street &&
analyzedData.get(i)[2] == streetNumber &&
analyzedData.get(i)[3] == city) && (i+1 ==
livingData.size())) {
categorize();
addToAnalyzedData();
break;
}
}
}
}
My question is that efficient enough to use it for really big data? Like 100.000 rows and more? Because I'm not about the if else statements. Could anybody help me?
String comparison works via equals, not == (How do I compare strings in Java?). Next point: This looks like the implementation in java of a plain SELECT DISTINCT * FROM someWhere-statement in SQL. So why not simply outsource the code to a database? If that's not possible, a Set would be most likely the most efficient collection. Though i'd recommend SQL to improve performance a lot and save resources on your local PC. One final note: Modifying data in a loop over the same data like here:
int analyzedDataSize = analyzedData.size();
for (int i = 0; i <= analyzedDataSize; i++){
...
addToAnalyzedData();
is extremely prone to bugs/exceptions. For e.g. you're retrieving and modifying a collection in the loop mentioned above, without updating the size of the collection. In this example, this behavior won't do any damage, but you should handle this rather carefully.
I have a scenario in which I need to send bulk emails to senders. I can send only 10 mails at a time.
So I set all my mail contents in variable and then add only ten recipients at a time.
Then after I send email, I again add ten next receipients to it.
private void addRecipients(Message pMessage, List pRecipients, Message.RecipientType pType, int pNum, int pOffset, int pBulkSize){
for (int i = 0; i < pRecipients.size(); i++) {
int offset = pNum + i;
if (pBulkSize != 0 && (offset < pOffset || offset >= pOffset + pBulkSize)) {
continue;
}
Address a;
Object r = pRecipients.get(i);
pMessage.addRecipient(pType, a);
}
But the problem is I am not able to remove the last ten receipients from the variable.
Transport.send(message);
After this line, my loop restarts and again the addRecipients method is called. But now the object
MimeMessage message = getIntialEmailConfiguration();
message has previous recipients as well.
I want to know how to reset that one property and remove existing recipients.
Use the Message.setRecipients which will remove and apply multiple addresses.
Your code example won't compile because 'Address a' is never assigned a value.
Hey so I have this homework assignment and I'm having issues with one of the methods. I would like hints and not actual answers/code.
So I have a class called HorseBarn that messes with an array of horses(horse being the type). My problem is I'm having troubles with the consolidate method.
What the array would look like before consolidate:
a,b,c,d are horses
|a|null|b|null|c|d|
What the array would look like after consolidate:
|a|b|c|d|null|null|
So my logic would be to make a nested for loop. The first loop would search for a null value, once the first loop finds the null value, the second loop would look for a horse and then swap with it. Then the second loop would end and go back to the first loop. So here is what I have right now and it doesn't work(it just terminates). Is my logic wrong or is it my syntax that's causing the problems?
public void consolidate()
{
int j = 0;
for(int i = 0; i < spaces.length;i++)
{
if( spaces[i] == null)
{
for(j = i; j < spaces.length && spaces[j] == null; j++)
{
}
spaces[i] = spaces[j];
spaces[j] = null;
}
}
Well for starters, this should give an index out of bounds exception if the last non-null is found and there still are elements remaining:
ex: horses = | a | null | null | null |
for i = 1, since horses[1] -> horses[3] are empty, j first gets set to 1 then ends with j = 4 (because of the termination condition j < horses.length())
You would then try to swap horses[1] with horses[4], which throws the array index out of bounds
In the inner for loop, just find the position of next non null value and break it there.
Then swap it with your null.
A better time efficient code.
I am trying to use an iterator on an ArrayList ( to get rid of a for loop, don't ask me why... ), however I need to skip the process of one of the arrays upon a boolean condition , should I still use an index and a break ???
// INTERPOLATION
int i = 0;
Iterator<CircularFifoQueue<SensorEvent>> buf = samplingFifoQueues.iterator();
while (buf.hasNext()) {
if ( i == 2 && !mDeviceSensorGyro) { // skip this queue if no gyroscope in device
break;
}
// proceed
buf.next();
i++;
}
thanks for help
What about this:
// INTERPOLATION
int i = 0;
Iterator<CircularFifoQueue<SensorEvent>> buf = samplingFifoQueues.iterator();
while (buf.hasNext()) {
if ( i != 2 || mDeviceSensorGyro) { // skip this queue if no gyroscope in device
// proceed
}
buf.next();
i++;
}
But I would rather attach some attribute to the queue elements to check for it. Work directly with numbers is bad practice.
I keep receiving a NullPointerException while trying to get a string from any array (that is encapsulated within a Vector). I cannot seem to stop the error from happening. It's got to be something simple, however I think that I have been looking at it for too long and I could sure use another set of eyes. Here is my code:
Vector<Event> details = vector.get(i).getEvent();
for (int x = 0; x < details.size(); x++) {
Event eDetails = details.get(x);
person = eDetails.getEventPerson();
place = eDetails.getEventPlace()[0];
time = eDetails.getEventTime()[0];
}
So when I try to get the item at position 0 in the array (when x is 0) that is returned from eDetails.getEventTime, a NullPointerException is thrown.
Now, when x is 0 I happen to know that the array element at position 0 of the getEventTime() array is an empty string, but it is NOT a null value. When x is 1 or 2, etc. I can retrieve the time just fine.
The problem is that I will still receive the NullPointerException when I try to do things like the following:
**System.out.println(eDetails.getEventTime.length);**
or
String result;
**if(eDetails.getEventTime[0] == null){**
result = "";
} else {
result = eDetails.getEventTime[0];
}
Any ideas?
Thanks!
Are you sure in your second example, it shouldn't be:
if(eDetails.getEventTime() == null)
Instead of:
if(eDetails.getEventTime[0] == null)
Are you making sure you leave the [0] off when you do the null check?
If the function eDetails.getEventTime() returns null, then you'll get a NullPointerException when you try to do eDetails.getEventTime()[0];
Seems that when you get details.get(0).getEventTime() the array returned is null.
The simplest way to figure this out is:
Vector<Event> details = vector.get(i).getEvent();
for (int x = 0; x < details.size(); x++) {
Event eDetails = details.get(x);
if (eDetails == null) {
throw new NullPointerException("eDetails on pos " + x + " is null");
}
person = eDetails.getEventPerson();
Something[] places = Details.getEventPlace();
if (places == null) {
throw ....
}
place = eDetails.getEventPlace()[0];
Something[] times = eDetails.getEventTime();
if (times == null) {
throw ....
}
time = eDetails.getEventTime()[0];
}
It may not look nice but at least it's informative.