Assuming your Car class has a getter method for price, you can simply use
System.out.println (car.get(i).getPrice());
where i is the index of the element.
You can also use
Car c = car.get(i);
System.out.println (c.getPrice());
You also need to return totalprice from your function if you need to store it
main
public static void processCar(ArrayList<Car> cars){
int totalAmount=0;
for (int i=0; i<cars.size(); i++){
int totalprice= cars.get(i).computeCars ();
totalAmount=+ totalprice;
}
}
And change the return type of your function
public int computeCars (){
int totalprice= price+tax;
System.out.println (name + "\t" +totalprice+"\t"+year );
return totalprice;
}
Answer from Ankur on Stack OverflowAssuming your Car class has a getter method for price, you can simply use
System.out.println (car.get(i).getPrice());
where i is the index of the element.
You can also use
Car c = car.get(i);
System.out.println (c.getPrice());
You also need to return totalprice from your function if you need to store it
main
public static void processCar(ArrayList<Car> cars){
int totalAmount=0;
for (int i=0; i<cars.size(); i++){
int totalprice= cars.get(i).computeCars ();
totalAmount=+ totalprice;
}
}
And change the return type of your function
public int computeCars (){
int totalprice= price+tax;
System.out.println (name + "\t" +totalprice+"\t"+year );
return totalprice;
}
You haven't shown your Car type, but assuming you'd want the price of the first car, you could use:
public static void processCars(ArrayList<Car> cars) {
Car car = cars.get(0);
System.out.println(car.getPrice());
}
Note that I've changed the name of the list from car to cars - this is a list of cars, not a single car. (I've changed the method name in a similar way.)
If you only want the method to process a single car, you should change the parameter to be of type Car:
public static void processCar(Car car)
and then call it like this:
// In the main method
processCar(cars.get(0));
If you do leave it as processing the whole list, it would be worth generalizing the parameter to List<Car> - it's unlikely that you'll really require that it's an ArrayList<Car>.
You can use stream of Java8 for filtering required elements like:
List wantedList = theseSocks.stream()
.filter(e ->e.toString().contains(ipAddress))
.collect(Collectors.toList())
You're looping over the ArrayList and want to compare based on the String value. But looping like this will immediately also give you the index. Your loop should look something like this:
for (int i =0; i< theseSocks.size(); i++)
{
String currentSock = theseSocks.get(i);
if (currentSock.equals(ipAddress))
{
System.out.println("the element is " +currentSock);
break;
}
}
Or even with a forEach loop
for (String currentSock: theseSocks)
{
if (currentSock.equals(ipAddress))
{
System.out.println("the element is " +currentSock);
break;
}
}
The break is used to interupt the for loop once your element is found.
Additionaly, your if condition will cause a print of every element if the array contains the ipAddress you're looking for.
Edit And then when using java 8, you can also use streams as posted by others.
As many have already told you:
mainList.get(3);
Be sure to check the ArrayList Javadoc.
Also, be careful with the arrays indices: in Java, the first element is at index 0. So if you are trying to get the third element, your solution would be mainList.get(2);
Time to familiarize yourself with the ArrayList API and more:
ArrayList at Java 6 API Documentation
For your immediate question:
mainList.get(3);
All you have to do is:
myList.get(Index);
This would return you the type of Object you used while creating the ArrayList. In your case it will return a String. Hence, what you can do is:
String firstElement = myList.get(0); //This would return "Hello"
This also shows that ArrayList indices start with 0
String a = myList.get(0); //a = "hello"
String b = myList.get(1); //b = "5"
Arrays are laid sequentially in memory. This means, if it is an array of integers that uses 4 bytes each, and starts at memory address 1000, next element will be at 1004, and next at 1008, and so forth. Thus, if I want the element at position 20 in my array, the code in get() will have to compute:
1000 + 20 * 4 = 1080
to have the exact memory address of the element. Well, RAM memory got their name of Random Access Memory because they are built in such way that they have a hierarchy of hardware multiplexers that allow them to access any stored memory unit (byte?) in constant time, given the address.
Thus, two simple arithmetic operations and one access to RAM is said to be O(1).
Posted as answer, as suggested:
ArrayList.get(int) does not search. It returns directly the element addressed by the index supplied... Exactly like with an array - hence the name.
ArrayList.get(int) source:
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
Where rangeCheck(int) is:
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
And elementData() is:
E elementData(int index) {
return (E) elementData[index];
}
A Linked List would however have an O(n) get: it would have to step to the next element until the desired index is reached...
public E get(int index) {
return entry(index).element;
}
Where entry(int) is (this is what makes it O(n)):
private Entry<E> More ...entry(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
Entry<E> e = header;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}
(Note: it is double linked list, so saves some time by selecting the endpoint that is closest to the desired result, but this is still O(n) )