我写的这段代码显然是正确的。问题在于变量 temp 始终为null。在第一个循环中,程序应输入第二个语句,但这不会发生。为什么?
ArrayList<Pair<Float, Float>> tmp = new ArrayList<>();
for (int i=0; (line = reader.readLine()) != null; i++) {
Float temp = null;
if (line.equals("----")) {
i = 0;
points.add(tmp);
tmp = new ArrayList<>();
} else if (i%2 == 0) {
temp = Float.parseFloat(line);
} else {
tmp.add(new Pair<Float, Float>(temp, Float.parseFloat(line)));
System.out.println("LINE: "+tmp.get(0).first);
}
}
PS:甚至IDE都说 temp 值始终为空
答案 0 :(得分:4)
您在Float temp = null;
循环中声明for
。因此,在循环迭代开始时,它将始终设置为null
。即使您打了第二条语句,temp
也会得到一个值,但是会立即被丢弃并使用值null
重新创建。
尝试在循环之前声明Float temp = null
。
答案 1 :(得分:0)
值temp并不总是为null。如果在解析后立即打印temp的值,则会看到此信息。
//
// DeanViewController.swift
// TestProj
//
// Created by Don Mag on 8/20/19.
//
import UIKit
class DeanViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let button1: UIButton = {
let b = UIButton()
b.translatesAutoresizingMaskIntoConstraints = false
b.backgroundColor = .red
b.setTitle("Button 1", for: .normal)
return b
}()
let button2: UIButton = {
let b = UIButton()
b.translatesAutoresizingMaskIntoConstraints = false
b.backgroundColor = .red
b.setTitle("Button 2", for: .normal)
return b
}()
let button3: UIButton = {
let b = UIButton()
b.translatesAutoresizingMaskIntoConstraints = false
b.backgroundColor = .red
b.setTitle("Button 3", for: .normal)
return b
}()
let filterPickerView: UIPickerView = {
let v = UIPickerView()
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let buttonStackView: UIStackView = {
let v = UIStackView()
v.translatesAutoresizingMaskIntoConstraints = false
v.axis = .vertical
v.spacing = 8
return v
}()
let containerView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .cyan
return v
}()
let pickerContainerView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .green
return v
}()
let scrollView: UIScrollView = {
let v = UIScrollView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .blue
return v
}()
let data1 = ["One", "Two", "Three"]
let data2 = ["This", "is", "a", "Test"]
let data3 = ["A", "B", "C", "D", "E"]
var modelData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// add the 3 buttons to the stack view
buttonStackView.addArrangedSubview(button1)
buttonStackView.addArrangedSubview(button2)
buttonStackView.addArrangedSubview(button3)
// add the stack view to the container view
containerView.addSubview(buttonStackView)
// add the container view to the scroll view
scrollView.addSubview(containerView)
// add the picker view to its container view
pickerContainerView.addSubview(filterPickerView)
// add the picker container view to the scroll view
scrollView.addSubview(pickerContainerView)
// add the scroll view to the view
view.addSubview(scrollView)
// setup the constraints
NSLayoutConstraint.activate([
buttonStackView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 20.0),
buttonStackView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -20.0),
buttonStackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 20.0),
buttonStackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -20.0),
filterPickerView.topAnchor.constraint(equalTo: pickerContainerView.topAnchor, constant: 20.0),
filterPickerView.bottomAnchor.constraint(equalTo: pickerContainerView.bottomAnchor, constant: -20.0),
filterPickerView.leadingAnchor.constraint(equalTo: pickerContainerView.leadingAnchor, constant: 20.0),
filterPickerView.trailingAnchor.constraint(equalTo: pickerContainerView.trailingAnchor, constant: -20.0),
containerView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 20.0),
containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20.0),
containerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0.0),
pickerContainerView.topAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 20.0),
pickerContainerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20.0),
pickerContainerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0.0),
pickerContainerView.bottomAnchor.constraint(greaterThanOrEqualTo: scrollView.bottomAnchor, constant: 20.0),
pickerContainerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor, constant: -40.0),
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
])
modelData = data1
filterPickerView.tag = 1
filterPickerView.dataSource = self
filterPickerView.delegate = self
button1.addTarget(self, action: #selector(btnTap(_:)), for: .touchUpInside)
button2.addTarget(self, action: #selector(btnTap(_:)), for: .touchUpInside)
button3.addTarget(self, action: #selector(btnTap(_:)), for: .touchUpInside)
}
@objc func btnTap(_ sender: UIButton) -> Void {
if sender == button1 {
modelData = data1
filterPickerView.tag = 1
} else if sender == button2 {
modelData = data2
filterPickerView.tag = 2
} else {
modelData = data3
filterPickerView.tag = 3
}
filterPickerView.reloadAllComponents()
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var btn: UIButton?
if pickerView.tag == 1 {
btn = button1
} else if pickerView.tag == 2 {
btn = button2
} else {
btn = button3
}
btn?.setTitle(modelData[row], for: .normal)
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return modelData[row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return modelData.count
}
}
如果您打算在下一次迭代中使用一次迭代的temp值,最好不要每次都将变量设置为null。