我有一个大小为20的数组,但是当我运行我的代码时,它只输出3个值。如何在数组中输入20个值?
public class LoopsMaybe
{
public static void main(String [] args)
{
int[] lott = new int[20];
for( int i=5; i<lott.length;i=i+7)
{
System.out.println(i);
}
}
}
输出: 五 12 19
我希望它打印出5,12,19,26,33,40,47 .....并停在序列中的第20个数字
答案 0 :(得分:2)
对于那些对Java 8细节感兴趣的人来说,完成这项工作的一种非常简单明了的方法是:
int seed = 5, inc = 7, many = 20;
IntStream is = IntStream.iterate(seed, n -> n + inc);
is.limit(many).forEach(x -> System.out.print(x + " "));
产出:5 12 19 26 33 40 47 54 61 68 75 82 89 96 103 110 117 124 131 138
答案 1 :(得分:0)
如果你必须使用数组,那么你需要其他东西来执行增量7。这样你可以保证20次迭代。
public class LoopsMaybe
{
public static void main(String [] args)
{
int[] lott = new int[20];
int inc=5;
for( int i=0; i<lott.length;i++)
{
System.out.println(inc);
inc += 7;
}
}
}
答案 2 :(得分:0)
public class ProductListAdapter extends BaseAdapter implements Filterable {
List<ProductModel> mStringFilterList;
ValueFilter valueFilter;
int item_number = 0;
TextWatcher tt = null;
private Activity activity;
private LayoutInflater inflater;
private List<ProductModel> modelItems;
private OnAddRemoveListener myaddInterface;
TextView tvTitle;
Button minusIcon, addIcon;
public ProductListAdapter(Activity activity, List<ProductModel> modelItems, OnAddRemoveListener myaddInterface) {
this.activity = activity;
this.modelItems = modelItems;
mStringFilterList = modelItems;
this.myaddInterface = myaddInterface;
}
@Override
public int getCount() {
return modelItems.size();
}
@Override
public Object getItem(int location) {
return modelItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}/*
@Override
public boolean isEnabled(int position) {
return false;
}*/
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.product_item_row, null);
final ProductModel m = modelItems.get(position);
tvTitle = (TextView) convertView.findViewById(R.id.tv2);
TextView tprice = (TextView) convertView.findViewById(R.id.tv3);
boolean ignoreNextTextChange = true;
final EditText tvItems = (EditText) convertView.findViewById(R.id.num);
tvItems.setText("0");
minusIcon = (Button) convertView.findViewById(R.id.minus);
addIcon = (Button) convertView.findViewById(R.id.add);
//add items
addIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("add button", "clicked");
int new_item_number = 1 + Integer.parseInt(tvItems.getText().toString());
tvItems.setText("" + new_item_number);
(myaddInterface).onItemadded(String.valueOf(m.getPrice()), new_item_number, position);
}
});
minusIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("minus button", "clicked");
int new_item_number = Integer.parseInt(tvItems.getText().toString());
if (new_item_number > 0) {
(myaddInterface).onItemremoved(String.valueOf(m.getPrice()), position);
tvItems.setText("" + (new_item_number - 1));
} else if (tvItems.getText().toString().equals("0")) {
tvItems.setText("0");
}
}
});
tvTitle.setText(m.getname());
tprice.setText("(" + m.getPrice() + ")");
public void afterTextChanged(Editable s) {
if (tvItems.getText().toString().trim().length() == 1) {
//to check if input is greater than 0 so we can proceed
if (Integer.valueOf(tvItems.getText().toString().trim()) > 0) {
//displays toast
Toast.makeText(activity, s, Toast.LENGTH_SHORT).show();
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
tvItems.addTextChangedListener(tt);
return convertView;
}
//code for search filter, not associated with the question
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<ProductModel> filterList = new ArrayList<>();
for (int i = 0; i < mStringFilterList.size(); i++)
if ((mStringFilterList.get(i).getname().toUpperCase())
.contains(constraint.toString().toUpperCase()) || (mStringFilterList.get(i).getproductDesc().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
ProductModel m = new ProductModel(mStringFilterList.get(i)
.getProduct_id(), mStringFilterList.get(i)
.getname(), mStringFilterList.get(i)
.getPrice(), mStringFilterList.get(i)
.getimage_url(), mStringFilterList.get(i)
.getproductDesc());
filterList.add(m);
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
//show no results
}
/* }else if((mStringFilterList.get(i).getproductDesc().toUpperCase())
.contains(constraint.toString().toUpperCase())){}*/
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
modelItems = (List<ProductModel>) results.values;
notifyDataSetChanged();
}
}
}