1.java indexOf()
2.javaä¸indexOf();ç使ç¨ã
3.java ä¸å¦ä½ä½¿ç¨INDEX OF()
4.java 中ArrayList集合用indexof方法返回元素的充值返红包源码索引 如果元素是重复的 应该返回哪个索引?
java indexOf()
åµåµ,æå°±ç±åçé¹,æç»ä¸ªæ´å¥½ç解å³æ¹æ³
å¿åæ¯æå«äººç¬è¯ææ¢å~好æ¹æ³ä¸è½ç¬èªäº«ç¨
//: IndexTest.java
public class IndexTest {
public static void main(String[] args) {
String test = "@@@";
char c = '@';
int index = 0;
int count =0;
//å个å符串æç´¢
while((index=nextIndex(test,c,index))>-1){
System.out.print(index+" ");
++index;
++count;
}
System.out.println("\nå ±æ¾å°å¹é '"+c+"' çå°æ¹ "+count+" å¤.");
index=0;
count=0;
String test2 = "abcabdabecdbdedfdecef";
String str = "de";
//å符串æç´¢
while((index=nextIndex(test2,str,index))>-1){
System.out.print(index+" ");
index+=str.length();//
++count;
}
System.out.println("\nå ±æ¾å°å¹é '"+str+"' çå°æ¹ "+count+" å¤.");
}
//ä¸ä¸ªå符
public static int nextIndex(String src,char search,int preIndex){
return src.indexOf(search,preIndex);
}
//ä¸ä¸ªå符串
public static int nextIndex(String src,String search,int preIndex){
return src.indexOf(search,preIndex);
}
}
javaä¸indexOf();ç使ç¨ã
解éåå¨æ³¨ééé¢ï¼public class Finallypractice {public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
a.add(0, "zero");
a.add(1, "one");
a.add(2, "two");
a.add(3, "three");
printAl(a); // æ¤æ¶açå ç´ æ¯ï¼zero one two three
if (a.contains("three")) {
a.add("four"); // æ¤æ¶açå ç´ æ¯ï¼zero one two three four
}
// remove(int index):移é¤æ¤å表ä¸æå®ä½ç½®ä¸çå ç´ (aä¸çç´¢å¼ä¸º2çå ç´ æ¯ï¼two)
a.remove(2);
printAl(a); // æ¤æ¶açå ç´ æ¯ï¼zero, one, three, fours
// indexOf: è¿åæ¤å表ä¸é¦æ¬¡åºç°çæå®å ç´ çç´¢å¼ï¼æå¦ææ¤å表ä¸å å«å ç´ ï¼åè¿å -1
if (a.indexOf("four") != 4) {
a.add(4, "4.2"); // æ¤æ¶açå ç´ æ¯ï¼zero one three four 4.2
System.out.println(a.indexOf("four")); // æ¤å表ä¸é¦æ¬¡åºç°fourçç´¢å¼æ¯3(ä»0å¼å§)
}
printAl(a);
if (a.contains("two")) {
a.add("2.2");
}
printAl(a);
}
public static void printAl(ArrayList<String> al) {
for (String element : al) {
System.out.print(element + " ");
}
System.out.println(" ");
}
}
java ä¸å¦ä½ä½¿ç¨INDEX OF()
public class Test{
public static void main(String[] args){
String s = "Hello World!";
// 1. indexOf(int ch)
System.out.println("s.indexOf('o')=" + s.indexOf('o')); //è¿å 4
//2. indexOf(int ch, int fromIndex)
System.out.println("s.indexOf('o',6)=" + s.indexOf('o',6)); //è¿å7
//3. indexOf(String str)
System.out.println("s.indexOf(\"llo\")=" + s.indexOf("llo")); //è¿å 2
//4. indexOf(String str, int fromIndex)
System.out.println("s.indexOf(\"llo\", 5)=" + s.indexOf("llo", 5)); //è¿å -1
}
}
å ·ä½indexOfçå®ä¹ï¼è¯·åèAPIææ¡£ã 满æ请é纳ã
java 中ArrayList集合用indexof方法返回元素的索引 如果元素是重复的 应该返回哪个索引?
以下是indexOf的源代码,可以看出, 是从0往后找,找到就返回
/
*** Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}