Arraylist for images (drawable) [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to show images upon clicking a button.
These images must come from an array so that i can easily add or delete images without having to edit all of my code.
However the compiler aborts my build and i have 2 errors.
public void showQuestion()
{
currentQuestion++;
imageNumber++;
if(currentQuestion == questions.length)
currentQuestion = 0;
if(imageNumber == myImageList.length)
imageNumber = 0;
questionView.setText(questions[currentQuestion]);
answerView.setText("");
answerText.setText("");
imagesview.setImageResource(myImageList[imageNumber]);
}
the first error is at imageNumber == myImageList.length, saying
Multiple markers at this line
length cannot be resolved or is not a field
myImageList cannot be resolved to a variable
the second error is on images view.setimageResource(myImageList[imageNumber]);
Multiple markers at this line
The type of the expression must be an array type but it resolved to ArrayList<Integer>
myImageList cannot be resolved to a variable
Edit:
The mistake i made was i overlooked that i was using an INT with the .length attribute. and a set instead of a .get!
Hope it helps

An ArrayList is of type list. Therefore .length should be .size() and myImageList[imageNumber] should be myImageList.get(imageNumber).

Related

If A is an array of type int, what command would you use to put the value 50 in the first position of the array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
If A is an array of type int, what command would you use to put the value 50 in the first position of the array
A[0] = 50;
This will store 50 in the first element of A.
A[0] = 50; This assign first element to 50.
There is a lot of resource about java array online, i think all the basic tutorial would cover about this.

how to get values from a Text view android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to store values from text view for example i am performing the following plus function like 5+6.Now i want to store 5 as value-1 and 6 as value-2.I only have one text view where i am displying like 5+6 in my project.I search a lot about my issue but could not found any proper solution. thanks
try this:
String string =text.getText().toString();
String[] separated = string.split("+");
String firstValue=separated[0];
String secondValue=separated[1];
You can try this and you will able to get the addition of two numbers at once. .
String[] getText = textview.getText().split("\\+");
int total=0;
for(String s:getText){
total+=Integer.parseInt(s);
}
in the end of iteration total will give the final result.From this method it gives the total of any numbers of numbers.

i not get second body on world in libgdx [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Array<Body> bodies = new Array<Body>(world.getBodyCount());
world.getBodies();
for (Body body : bodies) {
update(body);
}
Okay, first you're allocating an Array and ensuring the backing array will fit world.getBodyCount(). So, if the existing number of bodies in your world is ten, the ArrayList will have enough memory allocated to store ten bodies initially, but all of these entries will be null.
Then you call world.getBodies(); but this has no access to the local variable bodies, it's definitely not static; therefore your Array is still left uninitialized!
The Array is empty when you come to the for loop, so it execues zero times; nothing gets updated. So; the real trick is you're not accessing the bodies contained within the world properly.
Does getBodies() return a List you should be using instead?

Array Constants can only be used in initializers Error [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am new to Java Programming.I want to fetch data from the database and show it in JTable.
This is my current code.
String columns[]={"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
Object data[][]=new Object[200][5];
int i=0;
while(rs.next())
{
result_Transaction_id[i]=rs.getInt("f.Transaction_id");
result_User_name[i]=rs.getString("U.User_name");
data[i][5]={(Object)result_Transaction_id[i],(Object)result_User_name[i]};//This is where i get error
++i;
}
JTable View_table =new JTable(data,columns);
I want to collect the results of the query and add them in the specified object.
But i get the following error "Array constants can only be used in initializers".
I found a lot of solutions to "Array Constants can only be used in initializers" error, but those don't suit my case.
An "array constant" means { ... }.
As the error is trying to tell you, you can only use that syntax when declaring an array variable.
To assign an array to an existing expression, use new TypeName[] { ... }.
The error is the way you declare your array.
As stated here: https://stackoverflow.com/a/10520659/3558900 You can only declare an array NOT like this:
String columns[]={"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
BUT
String[] s;
s = new String[] {"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};

Removing elements from arrays? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I saw this snippet of code in a forum with the same question, however I needed 50 reputation to comment on the reply. So I posted it here:
array = ArrayUtils.removeElement(array, element);
I was wondering how this would be applied in code, as this is just the code:
Would it look something like this:
myArray = ArrayUtils.removeElement(myArray, 2);
or
myArray = ArrayUtils.removeElement(int[], 2);
The first line
myArray = ArrayUtils.removeElement(myArray, 2);
is correct syntax. It would return a new version of your array, with the element at index 2 removed. The other line would result in an error, because you aren't actually passing an array object but rather just a type.
Here's the JavaDoc for ArrayUtils if you would like to learn more about this method or other, related methods: https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html

Categories