import java.util.*;
public class SWDistaster extends ITDisaster
{
String Cause; int count=0;
List<String> SW_NameList = new ArrayList<String>();
List<String> SW_PlaceList = new ArrayList<String>();
List<Double> SW_CostList = new ArrayList<Double>();
List<Integer> SW_DateList = new ArrayList<Integer>();
public SWDistaster() {}
public SWDistaster(String Name)
{
super(Name);
}
public void show()
{
System.out.println(Name + " " + Place + " " + Cost + " " + Date + " " + Cause);
}
public void SW_SearchName(String name)
{
System.out.println(SW_NameList.lastIndexOf(name));
if(SW_NameList.contains(name))
{
System.out.println(SW_PlaceList.get(SW_NameList.indexOf(name)));
System.out.println("here");
}
else
{
SW_NameList(name);
//System.out.println("here2");
}
}
public void SW_NameList(String name)
{
SW_NameList.add(count, name);
count++;
}
public void SW_PlaceList(String place)
{
SW_PlaceList.add(place);
}
public void SW_CostList(double cost)
{
SW_CostList.add(cost);
}
public void SW_DateList(int date)
{
SW_DateList.add(date);
}
public void ShowList()
{
for(int i = 0; i<SW_NameList.size() ; i++)
{
System.out.print((SW_NameList.get(i)).toString());
}
}
}
和应用程序类:
public class Application {
public static void main(String[] args) {
SWDistaster dis1 = new SWDistaster();
dis1.SW_SearchName("Election");
dis1.ShowList();
SWDistaster dis2 = new SWDistaster();
dis2.SW_SearchName("Election");
dis2.ShowList();
SWDistaster dis3 = new SWDistaster();
dis3.SW_SearchName("Batee5");
dis3.ShowList();
}
}
在arraylist中没有任何附加内容,就像我在c ++中的向量中经常使用的那样! 我需要保存arraylist中的所有名称来搜索并且如果我找到它我将显示地点和日期,如果不是我将它添加到arraylist 并且任何人也可以帮助我如何使用文件更容易地保存在其中,因为我是java的新手
答案 0 :(得分:2)
请阅读last docsf here
的java文档public int lastIndexOf(String str,
int fromIndex)
返回指定子字符串最后一次出现的字符串中的索引,从指定的索引开始向后搜索。
返回的索引是最大值k:
k <= fromIndex && this.startsWith(str, k)
如果不存在这样的k值,则返回-1。
参数:
str - 要搜索的子字符串。
fromIndex - 从中开始搜索的索引。
返回: 指定子字符串最后一次出现的索引,从指定索引向后搜索,如果没有这种情况,则返回-1。