Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: ArrayList Help

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question ArrayList Help

    Hi All,

    I am very green with Java programming and I am in a class in which I have to answer some questions. The one question that will not compile correctly is:

    Assume that an ArrayList of integers named a has been declared and initialized. Write a single statement that assigns a new value to the first element of the ArrayList. The new value should be equal to twice the value stored in the last element of the ArrayList.

    This is the code that is provided to me:

     import java.util.*;
     
     public class CTest {
     	public static void main(String[] args) {
     		int j = 2;
     		ArrayList<Integer> a = new ArrayList<Integer>(Arrays.asList(343,43,65,34,564));
                    //<My Answer goes here>
    	}
    }

    Here's my answer to the question:

    		a[0] = 2 * a[a.length-1];

    Here's the compile errors I'm getting:

    Errors:

    CTest.java:8: error: cannot find symbol
    a.get[0] = 2 * a[a.length-1];
    ^
    symbol: variable get
    location: variable a of type ArrayList<Integer>
    CTest.java:8: error: cannot find symbol
    a.get[0] = 2 * a[a.length-1];
    ^
    symbol: variable length
    location: variable a of type ArrayList<Integer>
    CTest.java:8: error: array required, but ArrayList<Integer> found
    a.get[0] = 2 * a[a.length-1];
    ^
    3 errors



    I'm not sure why this is not working? Any help would be appreciated, because I really do not know much about this.

    Thanks,
    T
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,373
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: ArrayList Help

    the compile errors I'm getting:
    I assume you are trying to call the get() method.
    use () with method calls. [] is for arrays.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Help

    Quote Originally Posted by Norm View Post
    I assume you are trying to call the get() method.
    use () with method calls. [] is for arrays.
    Well...I've tried it a number of different ways. Here's what I have tired:

    a.set(0, 2*(a.get(a.length - 1)));

    and this:

    a[0]=a[(a.length)-1]*2;

    Both of these bring up compile errors.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,373
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: ArrayList Help

    a (a poor name for a variable that is supposed to hold some kind of data) is a reference to an ArrayList, not an array. When working with objects, you call their methods using ()s not []s.
    You should read the API doc for the ArrayList class to see what methods it has and how to use them.

    compile errors.
    When you get error messages you want help with, copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: ArrayList Help

    Quote Originally Posted by LugNut29 View Post
    Well...I've tried it a number of different ways. Here's what I have tired:

    a.set(0, 2*(a.get(a.length - 1)));

    and this:

    a[0]=a[(a.length)-1]*2;

    Both of these bring up compile errors.
    The ArrayList class doesn't override the [] operator. Hence you can't use it.

    Also, the ArrayList class doesn't have, at least not a public or protected one, called length. It has a method called size().

    Hence why you're getting compiling errors.

  6. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Help

    Quote Originally Posted by GoodbyeWorld View Post
    The ArrayList class doesn't override the [] operator. Hence you can't use it.

    Also, the ArrayList class doesn't have, at least not a public or protected one, called length. It has a method called size().

    Hence why you're getting compiling errors.
    Thank you for the help! I got it working! Here's what I used:

    a.set(0, 2*(a.get(a.size() - 1)));

    I'm all good now and I learned something new.

    T

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,065
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: ArrayList Help

    And you could have discovered these details yourself by reading the ArrayList API. Please try that next time.

  8. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Help

    Quote Originally Posted by GregBrannon View Post
    And you could have discovered these details yourself by reading the ArrayList API. Please try that next time.
    Wow...I've always been taught that if you don't know something, you ask questions. That way I can learn from others. I do not know programming, especially Java, and I'm trying to learn it. I'm sorry if I put anyone out by asking a question and wanting to get some advice. By telling someone to look at the API is indeed help, but maybe the API will not help the person the way it will for you? Don't you think I looked at the API, because I looked at it and I still could not figure out where I went wrong? Sorry! I didnít want you guys to do the work for me, I wanted help understanding where I went wrong. Different people learn in different ways. I was hoping someone would explain my mistake like GoodbyeWorld did. Once I read that explanation, I knew exactly where I when wrong.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,065
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: ArrayList Help

    Neither you nor GoddbyeWorld did anything wrong. All's fine, don't feel I'm criticizing.

    The goal of any teacher (we're not teachers) should be to give the student the tools and resources needed to explore and learn on their own. Frankly, the best teachers guide those who really want to learn to the paths that could lead them on a journey of self discovery, if taken. Yes, I suppose if you have a question, you should ask, but a valid and useful answer might have been, "Read the API." From there, you might have discovered the answer yourself and had a different sense of accomplishment. Whether that sense would have been greater or less varies from person to person, I suppose. For me, it would have been greater.

  10. #10
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Help

    Quote Originally Posted by GregBrannon View Post
    Neither you nor GoddbyeWorld did anything wrong. All's fine, don't feel I'm criticizing.

    The goal of any teacher (we're not teachers) should be to give the student the tools and resources needed to explore and learn on their own. Frankly, the best teachers guide those who really want to learn to the paths that could lead them on a journey of self discovery, if taken. Yes, I suppose if you have a question, you should ask, but a valid and useful answer might have been, "Read the API." From there, you might have discovered the answer yourself and had a different sense of accomplishment. Whether that sense would have been greater or less varies from person to person, I suppose. For me, it would have been greater.
    No worries...we're all good. I just wanted to express to you all that I wasn't looking for someone to do my work for me. I'm just trying to learn the way I know will help me best.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,373
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: ArrayList Help

    I've always found that when reading the API doc to resolve some specific problem, I see some other things that will come in handy in the future. End result is I solve the current problem and build some knowledge that will help in the future.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 03:44 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM

Tags for this Thread