This site requires JavaScript, please enable it in your browser!
Greenfoot back
2woodsway
2woodsway wrote ...

2011/11/12

One more problem

2woodsway 2woodsway

2011/11/12

#
Given an ArrayList a , write an expression that refers to the first element of the ArrayList. So, if the array a held the following values: 33,14,97,265, 84, then the value of your expression should be 33. I tried: Object obj = arrayList.get(33); What am I doing wrong?
bourne bourne

2011/11/12

#
An ArrayList contains a specific type of Object, that can be accessed by some index. So say you have an ArrayList called "myArray" that has the values : 33,14,97,265, 84 The following will return the value stored in the first position: myArray.get(0)
AwesomeNameGuy AwesomeNameGuy

2011/11/12

#
Arraylists are made with everythign in them null. To add an object to an arraylist you need to use the add method. To get somethign from them you need to use the get method. Also, I think your arraylist needs to know it is holding integers. So make your arraylist like this ArrayList<Integer> somelist = new ArrayList<Integer>(); remeber that ArrayLists can only hold objects, not primitives, so that's why you need the Integer class instead of int. To add 33 to it, use somelist.add(33); the complier will automatically convert the 33 to an Integer object, or you can use somelist.add(new Integer(33)), whichever you prefer. To get that object which in now the first element, remeber that the index starts from 0 so use Integer x = somelist.get(0); or you can use an int, java automatically converts ints to Integer objects and vice versa. But arraylists can't hold ints though. Well, hopefully that gets you somewhere.
2woodsway 2woodsway

2011/11/12

#
I don't understand please explain
2woodsway 2woodsway

2011/11/12

#
Thanks so much, I do not want to take too much of your time, I tried the following and it did not work: ArrayList<Integer> somelist.add(33); = new ArrayList<Integer>();
AwesomeNameGuy AwesomeNameGuy

2011/11/12

#
Step 1: import java.util.*; Step 2: make an arraylist somewhere in your program. Here's how. ArrayList<Integer> thelist = new ArrayList<Integer>(); Step 3: after you do step 2, add stuff to arraylist like this: thelist.add(33); // adds the number 33 to the ArrayList of Integers. It is in index 0. thelist.add(14); // adds the number 14. It is in index one. etc.. Step 4: get stuff from the arraylist like this: Integer x = thelist.get(0); // gets the first element from the arraylist, in this case, 33 Maybe you want to start leanring with more basic stuff like reference variables, objects, etc. Personally, if you are ever unfortinate enough to read my code, you will know that I love arraylists. But it can be a little confusing if you are absolutely new.
bourne bourne

2011/11/12

#
ArrayList<Integer> somelist = new ArrayList<Integer>(); somelist.add(33); I'm glad to answer your questions but, you should study more over whatever textbook or notes you have to better understand the concepts/syntax.
2woodsway 2woodsway

2011/11/12

#
Thanks so much. The only boo that was required for the class was greenfott. The course is online and help ins minimal.
2woodsway 2woodsway

2011/11/12

#
I am absolutely new..however your knowledge is inspiring. I hope one day to have jsust a small percentage of your knowledge.
bourne bourne

2011/11/12

#
Yeah just continue to pursue your passion for programming and you will get more than just a small percentage.
danpost danpost

2011/11/12

#
For information on variable arrays goto Arrays in Java (http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#17235)
You need to login to post a reply.