
list还可以点出remove(),clear(),get() 等方法。
你上百度问问题时尽量问简短一些比较好点,因为一般不会有人去给你一点一点去找,,
========-=======================================
package com.exam082
import java.util.ArrayList
import java.util.List
public class ArrayList02 {
public static void main(String[] args) {
ArrayList02 ay2=new ArrayList02()//创建这个类实例
ay2.testList()
}
private void testList() {
List<Integer>list=new ArrayList<Integer>()//拿到ArrayList集合类对象
list.add(11)//隐含了放的时候是11然后自动装箱把11变为Integer的对象类型的11了。
list.add(23)
for (int j = 0j <list.size()j++) {
//这样是可以的【list.get(j)是Integer对象类型的,这里自动拆箱为int类型的了】
int i = list.get(j)
System.out.println(i+100)//所以可以和100加了。
}
}
}
=============-========打印出下面的东西======================
111
123
题主可参考我的答案,亲测可用, 如果有什么不懂可追问
首先建立商品类:
public class Product {
/**
* 商品编号
*/
private String number
/**
* 商品名称
*/
private String name
/**
* 商品价格
*/
private float price
/**
* 商品数量
*/
private int quantity
public float getPrice() {
return price
}
public int getQuantity() {
return quantity
}
public Product(String number, String name, float price, int quantity) {
this.number = number
this.name = name
this.price = price
this.quantity = quantity
}
public void printProduct() {
System.out.println("商品编号:" + this.number)
System.out.println("商品名称:" + this.name)
System.out.println("商品价格:" + this.price)
System.out.println("商品数量:" + this.quantity)
}
}
2. 再建立测试类,直接运行main方法即可
public class ProductTest {
public static void main(String[] args) {
List<Product>productList = new ArrayList()
Product product1 = new Product("001", "商品1", 1f, 3)
Product product2 = new Product("002", "商品2", 1f, 1)
Product product3 = new Product("003", "商品3", 1f, 5)
Product product4 = new Product("004", "商品4", 1f, 2)
productList.add(product1)
productList.add(product2)
productList.add(product3)
productList.add(product4)
double total = 0
for (Product product : productList) {
product.printProduct()
total += product.getPrice() * product.getQuantity()
}
System.out.println("商品总价格:" + total)
}
}
答案如下
上面的两个类可以放在同一包下即可
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)