我有一个Arraylist可能包含或不包含重复的字符串,
list = ["car", "car", "car"] 要么 list = ["car", "car", "bike", "car"]
我想知道下面的内容,
if(list contains All same strings)
same = true
else if(list contains atleast one different element)
same = false
Run Code Online (Sandbox Code Playgroud)
请以书面形式帮我解决上述问题.
我下面的代码应该接受用户的整数,然后以相反的顺序打印他们输入的任何整数。我没有收到任何错误,但是当我运行该程序时,整数并未反向打印。有人可以帮我找出原因吗?
import java.util.*;
public class ReverseDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String response;
System.out.println("Please enter a whole number.");
response = sc.next();
reverseDigit(response);
}
public static void reverseDigit(String digit) {
ArrayList al = new ArrayList();
al.add(digit);
Collections.reverse(al);
System.out.println("Your number in reverse order is: " + al);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Java中使用2D数组列表.我有定义:
ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>();
Run Code Online (Sandbox Code Playgroud)
我如何循环并输入从1开始的数字?我知道我可以使用以下方法访问特定索引:
myList.get(i).get(j)
Run Code Online (Sandbox Code Playgroud)
哪个会得到价值.但是我如何添加到Matrix?
谢谢
我正在转换为数组编写的方法,用于查找从main传递的ArrayList的模式.我正在打击和索引错误的某个地方.我确定这是一个明显的错误,但只是没有看到它.会喜欢另一双眼睛!
public static int getMode(ArrayList<Integer> a)
{
int i ,j ,ctr=0 ,wantedScore ,maxsofar ,position=0 ,mode;
ArrayList<Integer> ctrArray = new ArrayList<>(a.size());
for(i=0; 1< a.size(); i++)
{
wantedScore = a.get(i);
for(j=i+1; j < a.size(); j++)
{
if(a.get(i)==wantedScore)
{
ctr++;
}//End IF
ctrArray.add(ctr);
}//End Inner Loop
}//End Outer For Loop
//Find highest value counter
maxsofar=a.get(0);
for(i=0; i< a.size(); i++)
{
if(ctrArray.get(i)>maxsofar)
{
maxsofar=ctrArray.get(i);
position=i;
}//End If
}//End For Loop
if(maxsofar>0)
mode=a.get(position);
else
mode=-1;
return mode;
}//End getMode
Run Code Online (Sandbox Code Playgroud)
如果Array列表包含值1,2,3,我得到的错误是:线程"main"中的异常java.lang.IndexOutOfBoundsException:Index:3,Size:3
我在String数组上有一个简单的循环,然后将String传递给threadlist方法.但是我似乎无法打印出两个String.它只打印第二个名称"Fred",这让我觉得我用第二个String覆盖了第一个String.我怎样才能ArrayList包括字符串"Tim"和"Fred"?
import java.util.ArrayList;
public class Threads extends Thread implements Runnable{
private ArrayList threadList;
private String e;
public static void main(String[] args) {
String[] elements = {"Tim","Fred"};
Threads t = new Threads();
for (String e: elements) {
t.threadL(e);
}
//loop over the elements of the String array and on each loop pass the String to threadL
for (int index = 0;index<t.threadList.size();index++){
System.out.print(t.threadList.get(index));
}
//loop over the threadList arraylist and printout
}
public ArrayList<String> …Run Code Online (Sandbox Code Playgroud) 我需要您的帮助,请您告诉我ArrayList中add()和set()有什么区别。我编写了一个使用set()和add()的程序,尝试找出试图找出哪些差异,这些差异是我在网上搜索的,但找不到合适的答案
public class arraylistDemo
{
public static void main(String[] args) throws Exception
{
ArrayList al = new ArrayList();
al.add(10);
al.add("A");
al.add("B");
al.add(null);
al.set(0, 11);
System.out.println("After Add "+""+al);
al.add(1, "AA");
System.out.println("Using add method"+ " " +al);
al.set(1, "AA");
System.out.println("Using set method"+ " " +al);
}
}
Run Code Online (Sandbox Code Playgroud)
O / P-使用加法[11,AA,B,null]使用设置方法[11,AC,B,null]
对于add()方法,实现使得elementData\[\]数组的大小根据需要增长.但是,查看该remove()方法时,它不会随着元素的移除而缩小尺寸.
我使用一个简单的代码测试,并elementData[]开始10和增长.但是,当我使用remove()方法删除所有元素elementData[]时,在我完成添加所有元素的位置停留的大小.
int testSize = 10000000;
ArrayList<Integer> alist = new ArrayList<Integer>();
// size of elementData[] is 10
for(int i = 0; i < testSize; i++) {
alist.add(i);
}
// size of elementData[] is 13845150
for(int i = alist.size()-1; i >= 0; i--) {
alist.remove(i);
}
// size of elementData[] remains at 13845150
Run Code Online (Sandbox Code Playgroud)
这不是浪费记忆吗?
这是我的代码:
dim myArrayList
function addName
Wscript.StdOut.WriteLine "What is your Quarterback's name?"
n = Wscript.StdIn.ReadLine
Wscript.StdOut.WriteLine "Attempts: "
a = Wscript.StdIn.ReadLine
Wscript.StdOut.WriteLine "Completions: "
c = Wscript.StdIn.ReadLine
Wscript.StdOut.WriteLine "Yards: "
y = Wscript.StdIn.ReadLine
Wscript.StdOut.WriteLine "Touchdowns: "
t = Wscript.StdIn.ReadLine
Wscript.StdOut.WriteLine "Interceptions: "
i = Wscript.StdIn.ReadLine
Set myArrayList = CreateObject( "System.Collections.ArrayList" )
myArrayList.Add n
myArrayList.Add a
myArrayList.Add c
myArrayList.Add y
myArrayList.Add t
myArrayList.Add i
end function
addname()
function show
for i = 1 to myArrayList.count
Wscript.StdOut.WriteLine myArrayList(i)
next
end function
show()
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:“ …
我有2个类员工和经理,可以扩展员工.我有Arraylist类型的员工,包含员工和经理.我想要通用方法,它将创建一个新的ArrayList类型管理器,它将复制第一个列表中的所有管理器.
我有两个ArrayLists:
ArrayList<String> testList = new ArrayList<>();
ArrayList<String> wordList = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
它们被传递给一个方法:
public static void binSearch(ArrayList a1, ArrayList a2)
Run Code Online (Sandbox Code Playgroud)
在方法内部,我想比较两个ArrayList之间的字符串:
a1.get(i).compareTo(a2.get(newEnd / 2))
Run Code Online (Sandbox Code Playgroud)
但是,我收到一条错误消息:
cannot find symbol
symbol: method compareTo(Object)
location: class Object
Run Code Online (Sandbox Code Playgroud)
我听说有些人说我需要实现/扩展Comparable,但似乎有些人在使用该方法时没有导入/实现/扩展任何东西.