2

I have written the following code to add the elements of the java. but i am unable to continue from one part here.

enter code here
import java.io.*;
import java.util.*;
class adder
{ 
public static void main(String[] args)
{
 List<Integer> list = new ArrayList<Integer>();
 int b;
 System.out.println("Enter the number");
 InputStreamReader inp = new InputStreamReader(System.in);
 BufferedReader bnp = new BufferedReader(inp);
 list.add(Integer.parseInt(bnp.readLine()));
  for(int i=0;i<list.size();i++)
  {
  b += list[i];
  }
   System.out.println("The answer is" + b);
 }
  }

 **OUTPUT**.
  add.java:15: array required, but java.util.ArrayList<java.lang.Integer> found
  b += list[i];

4 Answers 4

6

Java doesn't let you use array indexers on ArrayLists

 b += list[i];

Is illegal. Should be

 b+= list.get(i);

See the ArrayList API

Sign up to request clarification or add additional context in comments.

3 Comments

doesnt arraylist have an array feature? if not why is it called array list. can someone enlighten me on this
@Koneri List in C# does, in Java the design choice was not to add that feature so it's clear you're not working with an actual array.
@Koneri It's called ArrayList because probably this object stock your objects in an array (private transient Object[] elementData;) from the source code
0

This is because you are trying to access your List content as if it was an array (with list[i]). The proper way is to use : list.get(i).

Comments

0

You need to use .get() method in list to retrieve the elements from list

replace

   b += list[i];

with

 b+= list.get(i);

Comments

0

One major mistake that I see is that you have not initialized b variable. This is a local variable and must always be initialized before using. You code will not even compile as compiler won't allow this.

Another mistake is you add only one value to the List/Array(what ever you prefer) and then you try to iterate over the values stores. Makes no sense to me.

Next about your question, you seem to be confused between arrays and List. Either store your data in an array(in which case your for loop logic is correct) or else if you wish to use List then iterate over it to get your desired result.You can do something like the following -

int b = 0;
for(Integer number : list)
{
   b = b + number;
}
System.out.println("The answer is" + b);

Note : Internally this will also create an iterator of list and call .next() to get you all the values in the list. But I feel this is a smarter and better way.

3 Comments

Hi @Aniket , welcome to StackOverflow! In Java when you create a variable of type int and you don't explicitly initialize it, it defaults to 0 (see docs.oracle.com/javase/tutorial/java/nutsandbolts/… )
Hi @BenjaminGruenbaum that will only happen if the variable is instance variable and is true for any type(like String is initialized to null). My point is with respect to code mentioned in the above question. It is a local variables(inside main function) and it will not be initialized on it's own. In-fact the code will not compile if local variables are not initialized. If you try to compile it you will get The local variable b may not have been initialized error.
Oh right, you're talking about the local there. I misread, you're absolutely right of course, defaults apply to fields and not locals.

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.