假设我有一个ArrayList:
<string1.4>
<string2.4>
<string3.4>
Run Code Online (Sandbox Code Playgroud)
当我说arrayList.containsSubString('string1')时,我希望返回arrayList的第一个元素; 除了遍历每个元素arrayList并检查if是否string1是该元素字符串的子字符串之外,怎么做呢?
该计划一次又一次地做类似的事情.我想减少我使用的代码.这是一个例子,在这个例子中它不是必需的,但我将在未来做另外类似的事情.现在我很困惑并说:"我为什么要一个一个地复制相同的代码?"
但我得到'对象'不包含'ComputeHash'错误的定义.在运行时,它们不是对象,而是类.所以他们没有相同的变量类型.我该怎么办?
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.Filter = "ALL files(*.*)|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
textBox1.Text = String.Empty;
var list = new ArrayList() { crc32, md5, sha1 };
foreach (var checksum in list)
{
using (FileStream fs = File.Open(op.FileName, FileMode.Open))
{
foreach (byte b in checksum.ComputeHash(fs))
{
hash += b.ToString("x2").ToLower();
}
textBox1.Text += hash + "\r\n";
hash = String.Empty;
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我真的很困惑:我只是想把每个对象的名字添加ArrayList到另一个对象中ArrayList.
for (int i = 0; i < availableParts.size(); i++) {
for (int j = 0; j < namesOfIngredients.size(); j++){
if (availableParts.get(i).getName() != namesOfParts.get(j)){
namesOfParts.add(availableParts.get(i).getName());
}
}//middle if statement makes sure there are no repeats
}
Run Code Online (Sandbox Code Playgroud)
编辑:我意识到namesOfIngredients为null.但是,我需要它来启动null - 这就是我复制名称的方式.这可不是这样做的吗?
我正在尝试在Processing中使用ArrayList()方法.
我有这个:
ArrayList trackPoints = new ArrayList();
//inside a loop
int[] singlePoint = new int[3];
singlePoint[0] = 5239;
singlePoint[1] = 42314;
singlePoint[2] = 1343;
//inside a loop
trackPoints.add(singlePoint);
Run Code Online (Sandbox Code Playgroud)
所以基本上我想在我的ArrayList中添加一个带有三个值的数组"singlePoint".
这似乎工作正常,因为现在我可以使用println(trackPoints.get(5));,我得到这个:
[0] = 5239;
[1] = 42314;
[2] = 1343;
Run Code Online (Sandbox Code Playgroud)
但是,如何获得此数组的单个值?
println(trackPoints.get(5)[0]); 不起作用.
我收到以下错误:"表达式的类型必须是数组类型,但它解析为对象"
知道我做错了什么吗?如何从这个包含多个数组的arrayList中获取单个值?
谢谢您的帮助!
正如标题所说,我有两个ArrayLists.奇怪的是,在一个arraylist上设置一个值会改变另一个arraylist的值.
一些信息:这些是Entry类型的ArrayLists,每个都包含一个数量和一个值(这是你在括号中看到的).我试图改变这个数组:[(5,2)(2,5)(3,2)]
这些[(1,2)]和[(4,2)(2,5)(3,2)].在这种情况下,i == 1,remainingAmt == 1.
ArrayList<Entry> firstHalf = new ArrayList<Entry>();
ArrayList<Entry> secondHalf = new ArrayList<Entry>();
for(int j = 0; j<=i;j++){
firstHalf.add(rleAL.get(j));
}
for(int k = i; k<rleAL.size(); k++){
secondHalf.add(rleAL.get(k));
}
System.out.println(firstHalf);//gives (5,2)
firstHalf.get(i).setAmount(remainingAmt);
System.out.println(firstHalf);//gives (1,2) *CORRECT*
secondHalf.get(0).setAmount(rleAL.get(i).getAmount() - remainingAmt);
System.out.println(firstHalf);//gives (0,2) *WRONG*
Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList,我写的字符串:
blastScores = new ArrayList<String>();
blastScores.add("Hello");
blastScores.add("Yo");
prefs.edit().putString(CaptureActivity.BLAST_SCORES, blastScores.toString());
Run Code Online (Sandbox Code Playgroud)
每当我读入它时,它总是为空:
String blastScoresString = prefs.getString(CaptureActivity.BLAST_SCORES, null);
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
使用Java,我有一个类,它将网页检索为字节数组.然后我需要删除一些内容(如果存在).(该应用程序监视网页的变化,但需要从PHP创建的html中删除会话ID,并且意味着每次访问该页面时都会检测到更改).
一些生成的字节数组可能是1000个字节长的10.它们不是这样存储的 - 存储了16字节的MD5页面.但是,它是需要处理的原始全尺寸字节数组.
(更新 - 代码不起作用.请参阅下面的AH评论)显示我的代码的测试:
public void testSessionIDGetsRemovedFromData() throws IOException
{
byte[] forumContent = "<li class=\"icon-logout\"><a href=\"./ucp.php?mode=logout&sid=3a4043284674572e35881e022c68fcd8\" title=\"Logout [ barry ]\" accesskey=\"x\">Logout [ barry ]</a></li>".getBytes();
byte[] sidPattern = "&sid=".getBytes();
int sidIndex = ArrayCleaner.getPatternIndex(forumContent, sidPattern);
assertEquals(54, sidIndex);
// start of cleaning code
ArrayList<Byte> forumContentList = new ArrayList<Byte>();
forumContentList.addAll(forumContent);
forumContentList.removeAll(Arrays.asList(sidPattern));
byte[] forumContentCleaned = new byte[forumContentList.size()];
for (int i = 0; i < forumContentCleaned.length; i++)
{
forumContentCleaned[i] = (byte)forumContentList.get(i);
}
//end of cleaning code
sidIndex = ArrayCleaner.getPatternIndex(forumContentCleaned, sidPattern);
assertEquals(-1, …Run Code Online (Sandbox Code Playgroud) 我需要在arraylist或列表中创建数组,我使用了这个:
List<string[]> OL = new List<string[]>();
string[] OLdata = new string[7];
OLdata[0] = .. ; OLdata[1] = .. OLdata[6] = ..
OL.Add(OLdata);
Run Code Online (Sandbox Code Playgroud)
我可以用它来访问它
OL[0].GetValue(3) or OL[0][3] ..
Run Code Online (Sandbox Code Playgroud)
再次写入OLdata并将其添加到OL列表中,当我尝试访问数据时
OL[0][3] ..
Run Code Online (Sandbox Code Playgroud)
我正在获取插入数组的新数据,我显然期待OL [0] [3]和OL [1] [3]中的不同值,是什么原因或任何其他建议?
我需要获取对象的arrayList中最常出现的元素的计数.我有这个代码,它正在工作.
public static int contarRepeditos(ArrayList<Objecto> a) {
int resul = 0;
ArrayList<Integer> valores = new ArrayList<Integer>();
for (int i = 0; i < a.size(); i++) {
valores.add(a.get(i).getValor());
}
ArrayList<Integer> resultados = new ArrayList<Integer>();
for (int i = 0; i < valores.size(); i++) {
resultados.add(Collections.frequency(valores, a.get(i).getValor()));
}
resul = Collections.max(resultados);
return resul;
}
Run Code Online (Sandbox Code Playgroud)
我需要知道是否有最好的方法来做到这一点.谢谢.
我遇到了一些问题,因为我的方面很好地完成了这项工作:
for(Iterator<Object> i = mylist.iterator(); i.hasNext();) {
Object obj = i.next();
ArrayList<Object> newlist = new ArrayList<Object>();
newlist.add(obj);
i.remove();
for(Iterator<Object> in = mylist.iterator(); in.hasNext();) {
Object next = in.next();
if (obj != next && (another condition)) {
newlist.add(next);
in.remove();
}
}
if (newlist.size() > 2) anotheList.add(newlist);
else for(Object r : newlist) {
//mylist get back object:
mylist.add(r);
}
}
Run Code Online (Sandbox Code Playgroud)
我试图以一种优雅的方式来做到这一点,没有复制mylist越来越多次......
arraylist ×10
java ×8
arrays ×3
c# ×2
android ×1
class ×1
list ×1
object ×1
performance ×1
processing ×1