我希望组合框在运行时从数据库中存储名称,所以我创建了一个列表,但是组合框显示错误...
List<String> s = new ArrayList<String>();
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:project","sa","123456");
Statement stmt= con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT Name FROM company");
i=0;
while(rs.next()) {
s.add(rs.getString("Name"));
}
}
catch(Exception ex) { {
JOptionPane.showConfirmDialog(f,ex);
}
cb=new JComboBox(s);
}
Run Code Online (Sandbox Code Playgroud) if(cursor.getCount() !=0)
{
if(cursor.moveToFirst())
{
do
{
pObj.gritSize=cursor.getString(cursor
.getColumnIndex("grit"));
pObj.test = cursor.getString(cursor
.getColumnIndex("sieve"));
pObj.number = cursor.getString(cursor
.getColumnIndex("number"));
pObj.micro = cursor.getString(cursor
.getColumnIndex("micro"));
pObj.ret = cursor.getString(cursor
.getColumnIndex("retention"));
pList.add(pObj);
}while(cursor.moveToNext());
}
}
return pList;
Run Code Online (Sandbox Code Playgroud)
我有上面的代码从表中获取数据.我在pObj的每次迭代中得到正确的值.但是当我添加pList.add(pObj); 每次,它都会覆盖前一个条目并添加值.因此,在迭代结束时,我将最后一行作为arraylist中的每个项目.请帮忙.
我有一个HashSet<ArrayList<String>>名为possibleRoutes并希望将其转换为类型ArrayList<ArrayList<String>>.
你如何转换HashSet<Arraylist<String>>成ArrayList<ArrayList<String>>Java中?
我目前遇到以下代码错误.
import java.util.ArrayList;
public class Catapult
{
ArrayList<Double> distance;
Catapult()
{
ArrayList<Double> distance = new ArrayList<Double>();
}
public void calcDistance(Integer[][] data)
{
int counter = 0;
for(int a = 0; a < data.length; a++)
{
double speed = data[a][0];
for(int x = 0; x < data[0].length; x++)
{
double radian = Math.toRadians(data[0][1 + x]);
double dist = (speed * speed * Math.sin(2 *radian)) / 9.8;
distance.add(dist);
}
}
}
public void print()
{
for(int x = 0; x < distance.size(); …Run Code Online (Sandbox Code Playgroud) 我现在很困惑.我有一个ArrayList,我添加一个元素到ArrayList.现在ii尝试list.get(0)并抛出IndexOutOfBounds.......它变得更加奇怪:简而言之它看起来像这样:
ArrayList<SomeCustomClass> list = new ArrayList<SomeCustomClass>();
list.add(new SomeCustomClass());
int index = 0;
for(index = 0;index < list.size();index++){
if(list.get(index).something < somethingOther){ //Throws no error
break;
}
}
SomeCustomClass blarg = list.get(index); //Throws IndexOutOfBounds
Run Code Online (Sandbox Code Playgroud)
...为了确保,我在控制台中测试索引的值,它们都为零,但第二个引发错误.
提前致谢
编辑:Sry.我在问题中犯了错误.现在问题应该是正确的.是的:我检查索引是否有时超过0(它不是),如果"if"为真(它是)
编辑:搞笑:我手动改变指数为0,它是sais:
线程"Thread-2"中的异常java.lang.IndexOutOfBoundsException:索引:0,大小:0
我将0更改为1并且:
线程"Thread-2"中的异常java.lang.IndexOutOfBoundsException:索引:1,大小:1
(我只更改了错误来自的行中的值)Java是否在拖我?>.<:P
这是Java代码,我该如何使用C#
static ArrayList<ArrayList<String>> test= new ArrayList<ArrayList<String>>();
test.get(index).add(s[1]);
Run Code Online (Sandbox Code Playgroud) 所以假设我有一个迭代字符串列表的for循环.字符串列表就像
List<String> myString = {NEW ROW, cs, 1, 2, 3, NEW ROW, tp, 3, 4, 5}
Run Code Online (Sandbox Code Playgroud)
我希望for循环遍历列表并在每次NEW ROW出现在列表中之后删除NEW ROW和下一个x元素.
我怎样才能做到这一点?
我的尝试:
for (int index = 0; index < myList.size(); index++) {
if (myList.get(index).equals("NEW ROW")) {
for (int j = index; j < index + x; j++) {
myList.remove(j);
}
index = index + x;
}
}
Run Code Online (Sandbox Code Playgroud)
我的尝试不起作用.
您有自定义列表视图与以下代码段.
问题是当我运行代码时应用程序崩溃这是我的代码
class MyAdupter extends ArrayAdapter<String>{
Context context;
String[] title ;
String[] description;
int[] images;
public MyAdupter(Context context, String[] title, String[] description, int[] images) {
super(context, R.layout.my_simple_row,title);
this.context = context;
this.title = title;
this.description = description;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Layoutlnflater inflater = (Layoutlnflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.my_simple_row, parent, false);
ImageView imageView = (ImageView) row.findViewById (R.id.imageViewl);
TextView textTitle = (TextView) row.findViewById (R.id.textViewl);
TextView textDescription = (TextView) row.findViewById(R.id.textView2);
imageView.setImageResource(images[position]);
textTitle.setText(title[position]);
textDescription.setText(description[position]);
return row; …Run Code Online (Sandbox Code Playgroud) 我尝试创建一个固定的最后一项的ArrayList.例如,在ArrayList中[0,1,2,last],我试图在倒数第二个位置而不是最后一个位置添加一个项目.所以,如果我想在之前添加一个新项目(比如说3)last,我会做类似的事情,arraylist.add(3)它会给出输出[0,1,2,last,3],这显然不是我的要求.可以告诉我怎么做吗?
我有一个像这样的ArrayList;
int a=0;
int b=0;
int c=4;
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(a);
al.add(b);
al.add(c);
Run Code Online (Sandbox Code Playgroud)
我想删除所有条目至极具有价值0,并成为int c与价值4是在index 0.
这怎么样?