据http://download.oracle.com/javase/1.4.2/docs/api/java/util/Vector.html
Vector的iterator和listIterator方法返回的迭代器是快速失败的:如果在创建Iterator之后的任何时候对Vector进行结构修改,除了通过Iterator自己的remove或add方法之外,Iterator将抛出ConcurrentModificationException.因此,在并发修改的情况下,迭代器快速而干净地失败,而不是在未来的未确定时间冒任意,非确定性行为的风险.Vector的元素方法返回的枚举不是快速失败的.请注意,迭代器的故障快速行为无法得到保证,因为一般来说,在存在不同步的并发修改时,不可能做出任何硬性保证.失败快速迭代器会尽最大努力抛出ConcurrentModificationException.因此,编写依赖于此异常的程序以确保其正确性是错误的:迭代器的快速失败行为应仅用于检测错误
你能给我一个例子来验证上面的语句吗?我还不清楚向量的方法Iterator和ListIterator的失败快速行为.困惑: - ((
我在Core java中有一个问题.考虑具有名为age的属性的Employee类.
class Employee{
private int age;
public void setAge(int age);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我如何限制/阻止setAge(int age)方法使得它只接受正数并且它不应该允许负数,
Note: This has to be done without using client side validation.how do i achieve it using Java/server side Validation only.The validation for age attribute should be handled such that no exception is thrown
我昨天在接受采访时询问了以下2个问题
1>给定一个字符串,计算一个新字符串,其中原始字符串中相邻的相同字符之间用"*"分隔.
示例如下所示::
function name ispublic String pairStar(String str)
pairStar("hello") ? "hel*lo"
pairStar("xxyy") ? "x*xy*y"
pairStar("aaaa") ? "a*a*a*a"
Run Code Online (Sandbox Code Playgroud)
2>给定一个字符串,计算一个新的字符串,其中所有小写的"x"字符都已移动到字符串的末尾.
示例如下所示::
function name ispublic String endX(String str)
endX("xxre") ? "rexx"
endX("xxhixx") ? "hixxxx"
endX("xhixhix") ? "hihixxx"
Run Code Online (Sandbox Code Playgroud)
我不知道如何完成给定的问题,并努力解决这个问题
我有以下问题
给定一个字符串,返回一个"清理"字符串,其中相同的字符相同的字符已减少为一个字符.所以"yyzzza"产量"yza".
stringClean("yyzzza") ? "yza"
stringClean("abbbcdd") ? "abcd"
stringClean("Hello") ? "Helo"
Run Code Online (Sandbox Code Playgroud)
我正在尝试输入代码 stringClean("abbbcdd") ? "abcd"
我的代码在下面.我在进行相邻字符比较后得到部分附加字符串,因此截至现在我得到附加的stringBuilder "sb=abc",这不是正确的输出我应该得到输出"abcd",
class cleanString{
public static String stringClean(String str){
int startIndex = str.indexOf(str);
char startChar = '\u0000';
char adjacentChar = '\u0000';
System.out.println("startIndex-->" + startIndex);
final StringBuilder sb = new StringBuilder();
for(startIndex = 0; startIndex < str.length(); startIndex += 1){
startChar = str.charAt(startIndex);
System.out.println("startIndex ::" + startIndex);
System.out.println("startChar ::" + startChar);
final int adjacentPosition = startIndex + 1;
System.out.println("adjacentPosition …Run Code Online (Sandbox Code Playgroud) 我最近参加了Java面试问题并得到了以下查询,我不知道答案
1>我有下课
class CricketTeam{
String name; //this is the complete name(inclusive of first and last name)
}
Run Code Online (Sandbox Code Playgroud)
板球运动员的名字如下:
1> Sachin Tendulkar
2> Gautam Gambhir
3> Ricky Ponting
4> Shahid Afridi
5> Kevin Pieterson
6> MS Dhoni
我想通过他们的last name only.Suugestions /代码对上面的Cricket Players名称进行排序.我们将不胜感激.
2> 在java 中使用enhanced for loop反对
iterator有什么好处.在java中使用增强的for循环的优点是什么?为什么它可以在java工作时首先在java中引入iterator.
我需要在java代码中实现HashTable的内部实现.您还可以通过代码中的附加注释向我解释它是如何工作的.
我只掌握了HashTable中使用的负载系数和容量的一些基本知识,其中负载系数为0.75.你能用一个简短的例子来解释吗?
我很长时间都坚持这个.
1>为什么哈希表的加载因子为0.75而不是其他一些不同的值.很奇怪请澄清一下.
2>为什么我们没有HashMap的加载因子?
我不想要现有的代码.有人编写了比实际编写的代码更好的代码
需要java函数来查找字符串中最长的重复子串
For instance, if the input is “banana”,output should be "ana" and we have count the number of times it has appeared in this case it is 2.
解决方法如下
public class Test{
public static void main(String[] args){
System.out.println(findLongestSubstring("i like ike"));
System.out.println(findLongestSubstring("女士,我是亚当"));
System.out.println(findLongestSubstring("当生活递给你柠檬水时,做柠檬"));
System.out.println(findLongestSubstring("banana"));
}
public static String findLongestSubstring(String value) {
String[] strings = new String[value.length()];
String longestSub = "";
//strip off a character, add new string to array
for(int i = 0; i < value.length(); i++){
strings[i] = new String(value.substring(i)); …Run Code Online (Sandbox Code Playgroud)
圣诞快乐,并希望你在伟大的精神,我有一个Java-Arrays问题,如下所示.我坚持这个努力得到它的仪式.
Consider the leftmost and righmost appearances of some value in an array. We'll say that the "span" is the number of elements between the two inclusive. A single value has a span of 1. Write a **Java Function** that returns the largest span found in the given array.
**示例:
maxSpan({1,2,1,1,3})→4,答案是4 coz MaxSpan在1到1之间是4
maxSpan({1,4,2,1,4,1,4}) →6,答案是6考斯MaxSpan在4到4之间是6
maxSpan({1,4,2,1,4,4,4})→6,答案是6考克斯Maxspan在4到4之间是6大于4 MaxSpan在1和1之间,即4,因此6> 4答案是6.
我的代码不起作用,它包含给定元素的所有Spans,我无法找到给定元素的MaxSpan.
请帮帮我.
上述计划的结果如下所示
预期此运行
maxSpan({1,2,1,1,3})→4 5 X
maxSpan({1,4,2,1,4,1,4})→6 8 X
maxSpan({1,4, 2,1,4,4,4})→6 9 X
maxSpan({3,3,3})→3 5 X
maxSpan({3,9,3})→3 3行
maxSpan({3,9, 9})→2 3 …
从1 ..n添加数字时会调用什么.我知道当你把它们称为阶乘时
1* 2 * 3 * 4* 5 = 5!
Run Code Online (Sandbox Code Playgroud)
什么叫它什么时候
1+2+3+4+5 =15?
Run Code Online (Sandbox Code Playgroud) 我有一个HashMap如下(假设它有10,0000个元素)
HashMap<String,String> hm = new HashMap<String,String>();
hm.put("John","1");
hm.put("Alex","2");
hm.put("Mike","3");
hm.put("Justin","4");
hm.put("Code","5");
==========================
Expected Output
==========================
Run Code Online (Sandbox Code Playgroud)
Key = John",Value = "1"
Key = Alex",Value = "2"
Key = Mike",Value = "3"
Key = Justin",Value = "4"
Key = Code",Value = "5"
===========================
我需要Java代码来防止Addition of Duplicate <Key,Value> Pairs在HashMap中
满足以下条件.
1> hm.put("John","1"); is not accepted/added again in the Map
2>hm.put("John","2"); is not accepted/added again in the Map
希望清楚.提供的Java代码将不胜感激.(因为我可以向现有地图添加任何副本,所以需要通用解决方案)
问题陈述:
给定base和n均为1或更大,计算base的值为n次幂,因此powerN(3, 2)为9(3的平方).
例
powerN(3, 1) ? 3
powerN(3, 2) ? 9
powerN(3, 3) ? 27
Run Code Online (Sandbox Code Playgroud)
功能签名是 public int powerN(int base, int n)
我发现难以解决这个问题?帮帮我.
编辑:我需要一个不使用内置数学公式和递归的解决方案