4

I have small code where i have to add condition to get proper output.

if the element inside the mimeList is Last then go to else part. or to the if.

there is always 1 element in this ArrayList.

(if it has only one element then it means it is last element.)

for (int i = 0; i < mimeList.size(); i++) {
    StringBuffer sb = new StringBuffer();

    if () {
        queryString = sb.append(queryString).append(key)
        .append("=").append(mimeList.get(i)).append(" or ").toString();
    }else{
        queryString = sb.append(queryString).append(key)
        .append("=").append(mimeList.get(i)).toString();
    }
}
6
  • 1
    if (!(i == 0 || i == mimeList.size()))? Commented Aug 6, 2012 at 13:07
  • It's hard to understand what exactly you're asking... Commented Aug 6, 2012 at 13:07
  • @auser if is blank, must be where he wants code? Commented Aug 6, 2012 at 13:08
  • 2
    @AnthonyGrist i cannot be equal to mimeList.size() Commented Aug 6, 2012 at 13:08
  • Pasting your homework? Move StringBuffer out of the loop. Get rid of that ugly redundancy. Check that there's a space between keys. What about the "or" between first and second list item? Good luck. Commented Aug 6, 2012 at 13:09

8 Answers 8

11

Regarding the heading of your question:

get first and last element in ArrayList in Java

It should be pretty simple:

mimeList.get(0); // To get first
mimeList.get(mimeList.size()-1); //to get last

And regarding your if condition :

if(!(i==0 || i==mimeList.size()-1))

As you phrased it like:

if the element in mimeList is first or last it will go in else condition other wise in if condition

I used ! in if condition. Otherwise below is pretty cool:

if((i>0) && (i!=mimeList.size()-1))
Sign up to request clarification or add additional context in comments.

Comments

5

A simpler way to do with without lots of checks is to use seperator which is empty to start with.

StringBuilder sb = new StringBuilder();
String sep = "";
for (String s : mimeList) {
    sb.append(sep + key + "=" + s);
    sep = " or ";
}

2 Comments

:I have edited my question. Plz chk and help. if the element inside the mimeList is Last then go to else part. or to the if.
All you need is " or " between the key=value pairs. This is the simplest way to do that. You don't need an if and you don't need to repeat the code.
2

Something like so should work: if ((i > 0) && (i < mimeList.size() - 1)). Since in Java collections as 0 based, the first element will be at location 0 and the last will be at Collection.Size - 1, since accessing the Collection.Sizeth location will cause an out of bounds exception.

Comments

2

From what I can gather from your question, you want something like the following:

ArrayList<Foo> foos = fooGenerator.generateFoos();

for(int i = 0; i < foos.size(); i++) {
    if(i != (foos.size() - 1)) {
        System.out.println("Front elements of ArrayList");
    }
    else {
        System.out.println("Last element of ArrayList!");
    }
}

Comments

2
ArrayList<int> foos = ArrayList<int>();

for the first is

foos.get(0);

for the last is

foos.get(foo.size - 1);

Comments

1

Considering the i is Integer inside the ArrayList.

if ((i > 0) && (i < mimeList.size() - 1))

2 Comments

Technically i is an int (a primitive), not an Integer (an object).
@KumarVivekMitra I have edited my question. Plz chk and help.
0

You pretty much have the answer already in your code:

You loop from the first to the last element in your array.

first: having index 0, last: having index (size - 1)

This results in the following condition:

if ((i > 0) && (i < mimeList.size() - 1))

Comments

0

Try something like

if(i == 0 || i == mimeList.size() - 1)

Hope that helps!

1 Comment

Yeah, was a bit to quick there. Edited answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.