我想创建一个实现给定接口的对象集合(在C#中就是这样Collection<IMyInterface>).我不关心排序或索引访问,但希望用foreach(或Java等价物)轻松迭代集合.它还需要尽可能小的内存占用(理想情况下是线程安全).
您有什么推荐的吗?
谢谢
我们什么时候使用synchronized ArrayList?我们已经有了Vector同步.
我正在使用Java作为Web应用程序,而我正在使用MySql数据库.我需要在执行之前转义查询.这是我的实际代码:
db_result=mydb.selectQuery("SELECT nickname FROM users WHERE nickname='"+log_check_user+"' AND password='"+log_check_pass+"'");
public Vector selectQuery(String query) {
Vector v = null;
String [] record;
int colonne = 0;
try {
Statement stmt = db.createStatement();
ResultSet rs = stmt.executeQuery(query);
v = new Vector();
ResultSetMetaData rsmd = rs.getMetaData();
colonne = rsmd.getColumnCount();
while(rs.next()) {
record = new String[colonne];
for (int i=0; i<colonne; i++) record[i] = rs.getString(i+1);
v.add( (String[]) record.clone() );
}
rs.close();
stmt.close();
} catch (Exception e) { e.printStackTrace(); errore = e.getMessage(); }
return v;
} …Run Code Online (Sandbox Code Playgroud) 如果将元素逐个添加到数组列表中,例如:
ArrayList alist = new ArrayList();
alist.add("A");
alist.add("B");
alist.add("C");
alist.add("D");
Run Code Online (Sandbox Code Playgroud)
然后通过说alist.get(2)检索第三个元素,我保证检索我添加的第三个元素吗?
问题的第二部分,假设第一个问题的答案是肯定的:我什么时候使用ArrayList而不是Vector,反之亦然.
"数量是未知的"我的意思是,在创建我用来存储它们的任何数据结构时,我不知道最终将存储多少个对象.一旦我拥有了所有这些对象,我希望能够遍历所有这些对象,并且无论我访问它们的顺序都无关紧要.我想知道什么是最有效的(在时间和空间上,但主要是时间)在java中这样做的方法.
我做对的对象会出现在事情的最大数量上限,所以我正在考虑只是让这种大小的数组.但我不想浪费空间,阵列最终可能会比实际存储在其中的元素数量大两倍.
我也在考虑使用LinkedList,因为我认为迭代它可能比从一个像hashmap这样的东西创建一个迭代器并迭代它更有效.但我不知道从各种java数据结构创建迭代器有多昂贵.
那么,有什么想法吗?
所以,如果一个程序这样做:
static ArrayList<X> a = null;
static{
for(;;){X x = new X(); a.add(x)}
}
Run Code Online (Sandbox Code Playgroud)
在共享列表上静态初始化之后调用的唯一操作是get()和xt()
X x = a.get(i); x.t();
Run Code Online (Sandbox Code Playgroud)
和X没有访问容器并且是线程安全的,这应该意味着像这样使用Arraylist,没有同步是线程安全的,对吗?
在这种情况下,将数组的长度放入int中是否有任何好处 - 尽管很小 - 或者只是更好地调用array.length两次?或者编译器优化是否使这些方法等效?
没有int:
Vector<String> vs = new Vector<String>(a_o.length);
for(int i = 0; i < a_o.length; i++) {
vs.add(a_o[i]);
}
Run Code Online (Sandbox Code Playgroud)
INT:
int iLen = a_o.length;
Vector<String> vs = new Vector<String>(iLen);
for(int i = 0; i < iLen; i++) {
vs.add(a_o[i]);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用java来制作游戏.我想制作一种方法,可以轻松创建一个只有几行的世界.我想要一个像这样的方法......
public void makeLevel(see following*)
{
if (... == 1
{
drawGrass(0, 16);
}
......
}
Run Code Online (Sandbox Code Playgroud)
我想这样称呼它
makeLevel(1,2,1,1,2,2,1,3,2,1,2,2,1,2,1,2,3,2,2,2,2,1,2,1,2,2,1,2,1,2,1,2,3,2,1,2,3);
Run Code Online (Sandbox Code Playgroud)
我还将有一个名为drawGrass和drawWater的方法,它将使用Graphics2D绘制这些图片.
我不认为你需要我的代码,但如果你只是告诉我,我会把它放在这里.
我以为我可以以某种方式使用数组或类似的东西.
对于我工作的项目中的JPA-Entities,List或Map类型的属性总是初始化为同步实现Vector和Hashtable.
(Unsynchronized ArrayList和HashMap是Java中的标准实现,除非真的需要同步.)
有谁知道为什么需要同步收藏?我们使用EclipseLink.
当我问起这件事时,没有人知道为什么这样做.它似乎总是像这样完成.也许这是旧版EclipseLink所需要的?
我问的原因有两个:
示例实体:
@Entity
public class Person {
...
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable( ... )
private List<Role> accessRoles;
@ElementCollection
@CollectionTable( ... )
@MapKeyColumn(name="KEY")
@Column(name="VALUE")
private Map<String, String> attrs;
public Person() {
// Why Vector/Hashtable instead of ArrayList/HashMap?
accessRoles = new Vector<Role>();
attrs = new Hashtable<String, String>();
}
public List<Role> getAccessRoles() {
return accessRoles;
}
public void setAccessRoles(List<Role> accessRoles) {
this.accessRoles = accessRoles;
}
public Map<String, String> getAttrs() {
return attrs;
}
public void setAttrs(Map<String, String> attrs) …Run Code Online (Sandbox Code Playgroud) 我一直认为我们应该在Java中使用Vector,并且没有性能问题,这当然是正确的.我正在编写一种计算MSE(均方误差)的方法,并注意到它非常慢 - 我基本上是传递了值的向量.当我切换到Array时,速度提高了10倍,但我不明白为什么.
我写了一个简单的测试:
public static void main(String[] args) throws IOException {
Vector <Integer> testV = new Vector<Integer>();
Integer[] testA = new Integer[1000000];
for(int i=0;i<1000000;i++){
testV.add(i);
testA[i]=i;
}
Long startTime = System.currentTimeMillis();
for(int i=0;i<500;i++){
double testVal = testArray(testA, 0, 1000000);
}
System.out.println(String.format("Array total time %s ",System.currentTimeMillis() - startTime));
startTime = System.currentTimeMillis();
for(int i=0;i<500;i++){
double testVal = testVector(testV, 0, 1000000);
}
System.out.println(String.format("Vector total time %s ",System.currentTimeMillis() - startTime));
}
Run Code Online (Sandbox Code Playgroud)
其中调用以下方法:
public static double testVector(Vector<Integer> data, int start, int stop){
double toto …Run Code Online (Sandbox Code Playgroud) java ×10
arrays ×2
vector ×2
arraylist ×1
collections ×1
concurrency ×1
eclipselink ×1
jpa ×1
methods ×1
mysql ×1
optimization ×1
performance ×1