wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes ArrayList Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList Problem" Watch "ArrayList Problem" New topic
Author

ArrayList Problem

Lynn Finley
Greenhorn

Joined: Sep 29, 2002
Posts: 13
Here is the problem. I have to print 100 integers in an array list.
1,2,3,4,,5,6..............100
Then I need to print the elements starting from the middle of the list.
100,98,96,94��1��.93,95,97,99 ....
with 1 being in the middle.
Can anyone help me with this.
I found this method in Java Package, but I�m confused on how to use it
add
public void add(int index, Object element)
Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted
Here is the code I have so far�.Can anyone help�..

[ September 29, 2002: Message edited by: Lynn Finley ]

(Marilyn added code tags)
[ September 29, 2002: Message edited by: Marilyn de Queiroz ]
Lynn Finley
Greenhorn

Joined: Sep 29, 2002
Posts: 13
Please...does anyone know what I'm doing wrong.
Thanks in advance
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I don't think you need to use that method at all. It would seem that all you need to do is print the even numbers (use modulo) from 100 through 2, followed by printing the odd numbers from 1 through 99.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Lynn Finley
Greenhorn

Joined: Sep 29, 2002
Posts: 13
I've tried using mod division to come up with the answer but my output does not come out right. This is what I've used
for(int1=1;i<=100; i++)
{
if (i%2==0)
System.out.print(i)
else
if (i%2!=0)
System.out.print(i)
}
when I do this my output is 1,2,3,4,5 etc. starting with 1 at the beginning.
I just don't know how to make 1 start in the middle and the other numbers to correctly wrap around it.
Thanks for your help
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
try taking one of these:

followed by one of these:

[ September 29, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Lynn Finley
Greenhorn

Joined: Sep 29, 2002
Posts: 13
I've used the follwing code and it appears to work except, I have one problem...it prints my second arraylist multiple times...

Thanks for all of your help
[ October 01, 2002: Message edited by: Lynn Finley ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Two ways to tell that school is back in session... the leaves are falling from the trees and the nutty assignments start showing up in JavaRanch.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Let's look at the program:
You have created an ArrayList lst and filled it up
with 100 references to Integer objects.
The first Integer object contains 1, the second 2,
and so on up to the last Integer object containing 100.
You do not have to do any more to lst.
As I understand your problem you have to now
print it out in two ways.
The first way is from 1 to 100:

Notice the (Integer)lst.get(i-1). The indexes of the ArrayList really go from 0 to 99 not from 1 to 100 as our loop counter does. Also you have to cast the ArrayList elements from Object references back to Integer references.
That should result in a long line of output:
1,2,3,4,...,100
For the second way to print it you can use
the two loops:
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thomas said:
Two ways to tell that school is back in session... the leaves are falling from the trees and the nutty assignments start showing up in JavaRanch.

Yes, I noticed that too, but I got to get some real experience for SCJP
-Barry
Lynn Finley
Greenhorn

Joined: Sep 29, 2002
Posts: 13
Thank you so much for all your help
:-)
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
I have a general question toward Collections.
The Collections Framework provides the add()
method. When I try to override the add method
(like happended in the first version of TestArrayList with "public static void add(int 50, i)" then JVM throws this error:
TestArrayList.java:23: illegal start of expression
public void add(int 50, i) {
^
Why?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I'd bet that you have a syntax error in your code.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList Problem
 
Similar Threads
making a swap() method for a class DeckList that extends ArrayList
Reverse the order of elements in a ArrayList
Why I get the List content when I print the List object?
New ArrayList Problem
Collections.sort() throws ClassCastException