Answer the question
In order to leave comments, you need to log in
How to access the second value in an array?
There is an array:
private Brus[] arr = new Brus[3];
private int count = 0;
{
arr[0] = new Brus("Брусок", 500);
arr[1] = new Brus("Брусок1", 350);
arr[2] = new Brus("Брусок3", 5500);
count = 3;
}
public void calc() {
for (int i = 0; i < count; i ++) {
int calcTotalWeight;
}
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
First of all, I recommend using the search and only after that ask a question.
Regarding your question:
Let's say you have a class
class Brus {
public String name;
public String weight;
}
private Brus[] arr = new Brus[3];
, which, by the way, is dynamically initialized.{
arr[0] = new Brus("Брусок", 500);
arr[1] = new Brus("Брусок1", 350);
arr[2] = new Brus("Брусок3", 5500);
count = 3;
}
public void calc() {
int calcTotalWeight;
// получаем длину массива
for (int i = 0; i < arr.length; i ++) {
// получаем вес текущего блока
int weight = arr[i].weight;
// после получения веса текущего бруса суммируем его значение к общему весу calcTotalWeight
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question