我正在创建一个包含18个不同变量的类,其中大多数都是双倍的。我需要声明所有这些并且还对它们中的每一个执行相同的操作。有没有办法使用循环来循环所有声明? 例如,而不是说:
double a = 0.0;
double b = 0.0;
double c = 0.0;
...
double z = 0.0;
是否可以像这样循环它们:
while(variables are undefined)
define the next undefined variable
end loop
编辑:谢谢大家的答案。我一开始并不想使用数组,因为它会记住哪个元素代表哪个值更难。我将查看地图,如果这不起作用,我想我找到了一种方法来组织我能记住的数组。
答案 0 :(得分:2)
由于您使用的是所有相同类型的变量,并且您知道需要多少个变量,因此可以使用数组存储值,然后使用循环访问它们。
例如:
private List<Products> cartList;
private LayoutInflater inflater;
Context context;
public static final String URLL ="http://192.168.1.3/wordpress/upmeapi/class-woocommerce.php?function=remove_cart_api";
RequestObject requestObject;
public CartAdapter(Context ctx,List<Products> list){
this.context = ctx;
this.cartList = list;
}
@Override
public int getCount() {
return cartList.size();
}
@Override
public Object getItem(int position) {
return cartList.get(position);
}
@Override
public long getItemId(int position) {
Products c = cartList.get(position);
long id = c.getProductId();
return id;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
CartHolder holder = null;
if (row == null){
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.cartview_helper,parent,false);
holder = new CartHolder(row);
row.setTag(holder);
}
else {
holder =(CartHolder)row.getTag();
}
Products c = cartList.get(position);
Picasso.Builder builder = new Picasso.Builder(context);
Picasso picasso = builder.build();
picasso.with(context).cancelRequest(holder.myImage);
picasso.load(c.getProductImage())
.placeholder(R.drawable.ic_plusone_tall_off_client)
.resize(100,100)
.into(holder.myImage);
/* Picasso.with(context)
.load(c.getProductImage())
.placeholder(R.drawable.ic_plusone_tall_off_client)
.resize(100, 75)
.into(holder.myImage);*/
holder.title.setText(c.getTitle());
String stringdouble= Double.toString(c.getPrice());
holder.price.setText(stringdouble);
holder.quantity.setText(String.valueOf(c.getProductQuantity()));
holder.totalPrice.setText(c.getTotalPrice());
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long productId = getItemId(position);
//holder.button.setTag(position);
try {
ProductHelper productHelper = new ProductHelper();
productHelper.setProductId(productId);
ObjectMapper objectMapper = new ObjectMapper();
String req = objectMapper.writeValueAsString(productHelper);
requestObject = ExceptionRequest.generateRequest(req);
requestObject.setUrl(URLL);
new RemovefromList(position).execute(requestObject);
}catch (Exception e) {
e.printStackTrace();
}
}
});
return row;
}
static class CartHolder {
ImageView myImage;
TextView title;
//TextView descriptions;
TextView price;
TextView quantity;
TextView totalPrice;
Button button;
public CartHolder(View v){
myImage =(ImageView)v.findViewById(R.id.imageView2);
title =(TextView)v.findViewById(R.id.carttitle);
// descriptions =(TextView)v.findViewById(R.id.product_description);
price =(TextView)v.findViewById(R.id.cart_price);
quantity =(TextView)v.findViewById(R.id.item_quantity);
totalPrice =(TextView)v.findViewById(R.id.sub_total);
button =(Button)v.findViewById(R.id.remove_cart);
}
}
private class RemovefromList extends AsyncTask<RequestObject, Void,JSONObject> {
int selectedPos;
public RemovefromList(int pos){
selectedPos = pos;
}
@Override
protected JSONObject doInBackground(RequestObject... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// RequestObject requestObject = new RequestObject();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(arg0[0], ServiceHandler.POST);
JSONObject products = new JSONObject();
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
products = jsonObj.getJSONObject("rsBody");
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return products;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
try {
if (result!=null){
String status = result.getString("status");
cartList.remove(selectedPos);
notifyDataSetChanged();
Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return;
}
}
}
答案 1 :(得分:2)
如上所述,最好在C ++中使用数组或向量,如下所示:
std::vector<double> MyVector;
//Use 'push_back' to add values to the vector
MyVector.push_back(0.00);
要对矢量中的所有值执行操作,可以执行以下操作:
for (auto& i : MyVector)
someOperation(i);
在你的情况下,我们正在添加元素,所以我们可以使用这样的循环:
for (unsigned int i = 0; i < 20; ++i)
MyVector.push_back(some_value);
如果您知道需要多少个值,您也可以使用固定数组(否则通常更容易使用向量):
double MyArray[20] = {0}; //Creates an array of 20 doubles and sets them all to 0
//We can set individual values using the [] operator:
MyArray[0] = 2.3; //This sets the first element to 2.3
对数组的元素执行操作类似于我们对向量执行的操作:
for (unsigned int i = 0; i < 20; ++i)
someOperation(MyArray[i]);
类似地,用于定义带循环的数组:
for (unsigned int i = 0; i < 20; ++i)
MyArray[i] = some_value;
答案 2 :(得分:1)
使用map
或array
/ vector
。您可以使用它们循环遍历它们。
答案 3 :(得分:0)
你可能想尝试这个:
const list =Restangular.all('tasks').getList().then(function(result) {
$scope.tasks = result;
});
....
$scope.tasks[0].post()
答案 4 :(得分:0)
如果您无法更改结构,请将它们保留为变量并为其指定vector
:
class foo{
public:
foo(){
all.push_back(&a);
all.push_back(&b);
...
all.push_back(&z);
}
std::vector<double*> all;
private:
double a,b,c,...z;
};
然后你可以像:
int main(){
foo f;
for(auto& i:f.all){
*i=0.0;
}
}