我正在编写这个java程序,使用Sieve of Eratosthenes找到所有素数达到num,但是当我尝试编译时,它说我不能使用long var作为数组索引,并且它期望一个int var in它的位置.但我会使用大数字,所以我不能使用int.我能做什么?
import java.util.*;
import java.lang.*;
public class t3{
public static void main(String[] args){
long num = 100;
//declaring list and filling it with numbers
ArrayList<Long> numlist = new ArrayList<Long>();
for(long x=2 ; x<num ; x++){
numlist.add(new Long(x));
}
//sieve or eratosthenes
for(long x=0 ; x<Math.sqrt(num) ; x++){
for(long y=x+1 ; y<numlist.size() ; y++){
if(numlist[y]%numlist[x] == 0){
numlist.remove(y);
}
}
}
//print list
for(Object item : numlist){
System.out.println((Long)item);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个Android应用程序(Android 1.6),但这可能是一个更普遍的Java问题.
我有一个大约10,000个对象的ArrayList
对象包含3个字符串(firstName,middleName,lastName).
在android上向用户呈现"搜索框",他们可以通过键入部分名称来搜索特定的"对象".
我有一个类(我称之为Filterer),它在10,000个列表中搜索匹配的对象,然后将它们作为"子列表"返回.
搜索有点慢(特别是在Android手机上),我确信我没有以最有效的方式进行搜索/过滤.
有没有人对如何加快搜索有任何建议?我的代码如下.一种可能性来搜索已经包含小写和连接的每条信息的辅助"masterList"......但是可能有其他方法来改进这种搜索也会有所帮助.
TIA!
public void filterNames() {
this.filteredList.clear();
String sv = this.searchString.toString.trim().toLowerCase(); // search value
for (int i = 0; i < this.masterList.size(); i++) {
MyObject d = this.masterList.get(i);
String fn = d.getFirstName().toString().toLowerCase();
String mn = d.getMiddleName().toString().toLowerCase();
String ln = d.getLastName().toString().toLowerCase();
if (fn.indexOf(sv) >= 0 ||
md.indexOf(sv) >= 0 ||
ln.indexOf(sv) >= 0) {
this.currentList.add(d);
}
}
}
Run Code Online (Sandbox Code Playgroud) 是否可以在C#中创建多维arraylist?
StartDate Qty size
9/1/2010 10 15
9/1/2009 12 17
9/1/2008 11 19
Run Code Online (Sandbox Code Playgroud)
StartDate,Qty和size是3周的ArrayLists.我需要把它们放在一个arraylist中.我还需要对这个arraylist进行排序StartDate.这可能吗?除了arraylist之外,还有更好的方法吗?
我试图创建一个地图列表.在下面的代码中,我期待得到
[{start=1,text=ye}, {start=2,text=no}]
Run Code Online (Sandbox Code Playgroud)
但是,我只有
[{start=2,text=no}, {start=2,text=no}]
Run Code Online (Sandbox Code Playgroud)
如何避免覆盖第一张地图?这是我的代码:
HashMap mMap = new HashMap();
ArrayList list = new ArrayList();
list.add(new HashMap());
mMap.put("start",1);
mMap.put("text","yes");
list.add(mMap);
mMap.put("start",2);
mMap.put("text","no");
list.add(mMap);
System.out.println("Final result: " + list );
Run Code Online (Sandbox Code Playgroud)
谢谢!
==========================
作为一名来自程序语言背景(SAS)的Java学习者,我花了不少时间学习和试验ArrayList,LinkedList,Map,LinkedMap等等 - 我感到很冷静.我不明白为什么我的知识有限.现在,以下答案都很棒!他们在Java中解释了非常重要的数据结构,至少对我而言.
谢谢你们!!!!
对不起,伙计们,我删除了我的APPLES和CATS示例:)这是我的问题的更新版本!
我在这里失去理智.我需要能够启发我的人.我曾尝试过几次在这里解释我的问题.希望这次,我的问题会更容易理解.
基本上我有这个框架,并显示一个图像.右侧有一个JList,底部有另一个JLabel面板.这是我框架的屏幕截图.

当我点击图像时,会弹出一个JOptionPane,就像这样.我输入了我的输入.我的JList是一个ArrayList,所以我输入的所有内容都被添加到JList和底部的JPanel.

现在,当我将鼠标悬停在我点击的部分时,你注意到方形消失了).它仅在我单击图像时出现,并且当我将标签悬停在底部时.我的标签,截至目前是LOLZ NOSE和INPUT HERE.

我想要做的是当我将鼠标悬停在标签上时,例如INPUT HERE,它再次显示正方形,其中包含我点击的部分.我现在的问题是,当我点击NOSE时,它应该在鼻子部分显示一个正方形,并且名字NOSE有黑色bg,它不显示.此外,仅显示最后一个标签的正方形,而忽略其他标签的位置点击.
如何获得标签以记住我所做的点击的位置?人们说我应该使用ArrayLists或HashCodes但是我不知道如何实现它们.感谢任何能提供帮助的人.
编辑:我已经完成了矩形,顺便说一句.它仅显示输入的最后一个标签.以下是一些要求的代码片段!
我如何在JLabel上设置文本并更新JList:
public void updateLabel(){
StringBuilder text = new StringBuilder(); //creates empty builder, capacity 16
for(Object s: tagModel.toArray()) //returns an array containing the elements of the tagModel
text.append(" " + s);
repaint();
hoverLabel.setText(text.toString()); //returns a String
hoverLabel.addMouseMotionListener(this);
hoverPanel.add(hoverLabel);
}
Run Code Online (Sandbox Code Playgroud)
点击后我的mouseListener:
@Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
isRectPresent = true;
repaint();
input = JOptionPane.showInputDialog("Enter tag name:");
if((input != null) && …Run Code Online (Sandbox Code Playgroud) 有没有办法ArrayList用double类型定义?我试过了两个
ArrayList list = new ArrayList<Double>(1.38, 2.56, 4.3);
Run Code Online (Sandbox Code Playgroud)
和
ArrayList list = new ArrayList<double>(1.38, 2.56, 4.3);
Run Code Online (Sandbox Code Playgroud)
第一个代码显示构造函数ArrayList<Double>(double, double, double)未定义,第二个代码显示后面需要维度double.
在java.util.Arrays中,定义了一个名为"ArrayList"的私有静态类.它仅从Arrays.asList方法引用.
这样做有什么好处?为什么不引用java.util.ArrayList呢?
代码如下:
/**
* @serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
Run Code Online (Sandbox Code Playgroud) 嗨大家我得到一个无法在我的OnCreateView方法中解析构造函数ArrayAdapter,我listAdapter试图在我的选项卡中列出文本数据,这是我的主要活动.任何帮助将不胜感激我仍然是一个noobie in Java.
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
mPager= (ViewPager) findViewById(R.id.viewPager);
mTabs=(SlidingTabLayout) findViewById(R.id.tabs);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs.setDistributeEvenly(true);
mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
mTabs.setViewPager(mPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 我有一个类似于下面的方法:
public void addSubjectsToCategory() {
final List<Subject> subjectsList = new ArrayList<>(getSubjectList());
for (final Iterator<Subject> subjectIterator =
subjectsList.iterator(); subjectIterator.hasNext();) {
addToCategory(subjectIterator.next().getId());
}
}
Run Code Online (Sandbox Code Playgroud)
当它同时为同一个用户(另一个实例)运行时,有时它会抛出NoSuchElementException.根据我的理解,有时subjectIterator.next()在列表中没有元素时执行.仅在访问时会发生这种情况.方法同步会解决这个问题吗?
堆栈跟踪是:
java.util.NoSuchElementException: null
at java.util.ArrayList$Itr.next(Unknown Source)
at org.cmos.student.subject.category.CategoryManager.addSubjectsToCategory(CategoryManager.java:221)
Run Code Online (Sandbox Code Playgroud)
该堆栈跟踪失败addToCategory(subjectIterator.next().getId());.
我正在使用Codingbat网站,特别是AP-1中的这种方法
public String[] wordsWithout(String[] words, String target) {
ArrayList<String> al = new ArrayList<>(Arrays.asList(words));
al.removeIf(s -> s.equals(target));
return al.toArray(new String[al.size()]);
}
Run Code Online (Sandbox Code Playgroud)
这个实现工作,以及它当前提交的内容,但是当我将return语句更改为
return al.toArray(String[]::new);
Run Code Online (Sandbox Code Playgroud)
据我所知,这应该工作,给出以下错误:
no suitable method found for toArray((size)->ne[...]size])
method java.util.Collection.<T>toArray(T[]) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; Array is not a functional interface))
method java.util.List.<T>toArray(T[]) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; Array is not a functional interface))
method java.util.AbstractCollection.<T>toArray(T[]) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; Array is not …Run Code Online (Sandbox Code Playgroud)