如何向ArrayList添加数字,但是保持它们的添加顺序?
例)Arraylist包含:[5,3,9] ...我想添加[1,2,3]来修改列表= [1,2,3,5,3,9]
Ex2)Arraylist包含:[] ...我想添加[1,2,3]来修改列表= [1,2,3]
我将一直使用一个静态arraylist
用户将通过输入以下命令从命令行输入命令:"a X1 X2 X3 ... Xn"其中'a'是命令,X1-Xn是要添加到列表中的未知数量的整数
我是一名新手程序员,我正在用Java编写我的第一个游戏,我刚刚实现了一个目标AI,现在每当我运行它时,总是滞后于开始,我想解释为什么请或者某种方法这个更好,谢谢你提前.
代码:(对于最可能造成滞后的因素)
public class Handler {
//Use Linked Lists
private Animator a;
private boolean renderMini;
private int x, y;
public LinkedList<GameObject> object = new LinkedList<GameObject>();
public LinkedList<EntityObject> entity = new LinkedList<EntityObject>();
public LinkedList<Faction> faction = new LinkedList<Faction>();
public Handler(){
a = new Animator();
this.renderMini = false;
}
public void render(Graphics g){
///if(GameMain.numFrames > 5){
if(renderMini){
a.AnimateMini(g, x, y, 32, 32);
}
for(int i = 0; i < entity.size(); i++){
EntityObject tempObject = entity.get(i);
tempObject.render(g);
}
for(int i = 0; i …Run Code Online (Sandbox Code Playgroud) 我List<Point2D.Double> serie在绘图仪中用于存储点坐标。当我想排序时,我决定使用,Collections.sort(serie)但它表明存在类似的错误
不能有双重类型
那么如何List<Point2D.Double>按x坐标排序?
我在下面写了代码.它得到这样的信息:
线程"main"中的异常java.lang.ClassCastException:[D无法强制转换为java.lang.Double
double[] xyz = {1, 11, 111, 2, 22, 222};
ArrayList array = new ArrayList();
array.add(xyz);
double[] vals = new double[array.size()];
vals[0] = (double) array.get(0);
vals[1] = (double) array.get(1);
vals[2] = (double) array.get(2);
Run Code Online (Sandbox Code Playgroud)
我也搜索并看到Stack Overflow上的一些帖子,但它们并没有让我感觉到多少.我该怎么办?
targetPixValList是一个包含Double对象的列表,它在列表中的连续位置也包含类似的值,当我尝试使用Code_1比较两个Double值时,cnt返回零.当我使用code_2时,cnt返回值.
并且列表的类型是
为什么"=="运算符不适用于Double?
Code_1:
int cnt = 0;
for (int i = 0; i < cs.targetPixValList.size()-1; i++) {
if (cs.targetPixValList.get(i) == cs.targetPixValList.get(i+1))
++cnt;
}
Run Code Online (Sandbox Code Playgroud)
CODE_2:
int cnt = 0;
for (int i = 0; i < cs.targetPixValList.size()-1; i++) {
if (cs.targetPixValList.get(i).equals(cs.targetPixValList.get(i+1)))
++cnt;
}
Run Code Online (Sandbox Code Playgroud) 我似乎无法让它工作,我已经尝试了很多不同的东西,解析工作正常并返回一个arraylist中的标题列表,我无法让它们在ListView上查看.这是代码;
/**
* Created by Matt on 4/21/2015.
*/
public class Discussion_Fragment extends Fragment {
View rootview;
ListView listView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.discussion_layout, container, false);
ArrayList<String> titles = new ArrayList<String>();
listView = (ListView) rootview.findViewById(R.id.listView1);
parse(getHTTP("https://www.reddit.com/r/DotA2.json?limit=10"));
for(int i=0;i<titles.size();i++){
titles.add(titles.get(i));
}
ArrayAdapter saAdapter = new ArrayAdapter(getActivity(), R.layout.discussion_layout, titles);
listView.setAdapter(saAdapter);
return rootview;
}
public static String getHTTP(String urlToRead) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
boolean response …Run Code Online (Sandbox Code Playgroud) 我正在研究一个java项目,我遇到了问题.我想有两个表的总和a,并b在列表中c,但我不知道该怎么做.我希望a.add(3) +b.add(4)应该在下一个列表c中,值应为7,类似于5 + 2 = 6 1 +( - 4)= - 3任何建议和帮助将不胜感激
码:
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> a = new ArrayList<Integer>();
List<Integer> b = new ArrayList<Integer>();
List<Integer> three= new ArrayList<Integer>();
a.add(3);
a.add(5);
a.add(1);
a.add(-2);
b.add(1);
b.add(2);
b.add(-4);
b.add(3);
}
}
Run Code Online (Sandbox Code Playgroud) 我可以访问并打印我的arraylist中的字符串
list.get(0).getLastName();
Run Code Online (Sandbox Code Playgroud)
但我无法访问char:
list.get(0).getLastName().charAt(0);
Run Code Online (Sandbox Code Playgroud)
即使我设置list.get(0).getLastName()等于像str这样的变量,我也可以打印str,但如果我尝试str.charAt(0),它永远不会返回任何我可以打印或使用的东西.
任何帮助赞赏.
我ArrayList在Java中有一个简单的问题.
我有一个简单的ArrayList.现在我要检查,如果ArrayList包含某个字符串.我知道contains()将对象作为一个整体进行检查,但我想检查对象的一部分是否具有我正在寻找的字符串.
那么实现这一目标的最佳方法是什么?我的样本在这里说False,我希望它说True.怎么样?
ArrayList<String> test_al = new ArrayList();
test_al.add("hello world");
if(test_al.contains("hello")){
Log.v(LOG_TAG,"True");
}
else{
Log.v(LOG_TAG,"False");
}
Run Code Online (Sandbox Code Playgroud) 我有这个 ArrayList
public ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
HashMap<String, String> comparemap2 = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我想要的是:我想要ArrayList中的所有Items并希望将它们放入HashMap我的HashMap看起来像:
核心价值
job_id 032014091029309130921.xml
job_id 201302149014021492929.xml
job_id 203921904901920952099.xml
编辑: 稍后我想将此地图与现有地图进行比较:
Properties properties = new Properties();
try {
properties.load(openFileInput("comparexml.kx_todo"));
} catch (IOException e) {
e.printStackTrace();
}
for (String key : properties.stringPropertyNames()) {
compareMap.put(key, properties.get(key).toString());
}
HashMap<String, String> oldCompareMap = new HashMap<>();
for (HashMap key : xmlFileNames) {
oldCompareMap.putAll(key);
}
isEqualMaps(oldCompareMap, compareMap);
Run Code Online (Sandbox Code Playgroud)
我只想比较,如果文件名存在于compareMap.如果没有,请将其添加到xmlFileName地图中
我已经在StackOverFlow中查找了,我如何将ArrayList转换为HashMap.但是其他线程会将数据类型视为Item或Product.
我希望你能帮帮我!
亲切的问候
arraylist ×10
java ×9
android ×3
collections ×1
double ×1
for-loop ×1
list ×1
listview ×1
optimization ×1
performance ×1
sorting ×1
static ×1
string ×1
sum ×1
swing ×1