将ArrayList与String进行比较

时间:2013-10-20 07:00:36

标签: java arraylist

//我的问题是,在方法cancelOrder中,至少没有看到字符串[] waitingList [],这就是我的想法。

public static String[] canceledOrder(String[] waitingList,String[] waitingList1,String []waitingList2,String[] waitingList3){//I've decided to pass these string [] hoping the string from the other methods will now be seen in canceledOrder();
            Scanner in = new Scanner (System.in);
            int option;
            System.out.println("Select the event you want to cancel :\n");
            events();
            option= in.nextInt();
            in.nextLine();
            System.out.println("Person on wait list is " + waitingList[name] );
            switch (option){

            case 1:
                System.out.println("Please enter your name:\n");
                canceledname = in.nextLine();
                System.out.println("name:" + canceledname);



                for (String s : myStringList) {
                    if(s.equals(canceledname)){

                        s = waitingList[name];

                        System.out.println("The new name is\n" + s);

                        name++;
                    }
                    return s; // I want it to now return waitingList[name]
                }

                break;

3 个答案:

答案 0 :(得分:0)

我想你想知道canceledname中是否存在myStringList,因为你必须遍历myStringList - 无法绕过它。

for(String name : myStringList){
    if(name.equals(canceledname){
          // continue...
          k = waitingList[name];
          System.out.println("The new name is" + k);
          i++;
          name++;
    }
}

对于那种比较,最好将名称保存在Set中,例如:

HashSet<String> names = new HashSet<String>();
// here you'll add the names to the hash-set
// using: names.add("whatever");
// ...

//and now:
if(names.get(canceledname) != null){
      // continue ...
}

答案 1 :(得分:0)

我可能错了,但您是否想要遍历arraylist以查看列表中的任何值是否等于cancelledName

如果是这样,你可能想要这样的东西

for (String s : myStringList) {
    if(s.equals(canceledname)){

        k = waitingList[name];

        System.out.println("The new name is" + k);

    name++;
    }
}

编辑:可能解决问题

// declare array of Attendees, capacity is 20
// I would rather use ArrayList, but it seems you're trying to use array
String attendees[] = new String[20];

// delcare wait list
String waitList[] = new String[2];

// declare int for tickets sold
int ticketsSold = 0;
// declare waitlist
int waitlistCount = 0;

// prompt buyers for purchase of tickets
// ticketsSold = ticketsSold +  1 or 2 depending on tickets
// if ticketsSold is 20 do not sell tickets and send to waiting list
// Do this until full

// if ticketsSold = 20
// prompt user to go on wait list
// if yes, ask how many tickets
// if tickets requested for waitlist exceeds current waitlist + tickets requested
       // System.out.println("Sorry, waitlist is full");
// else prompt for user name
String name = in.nextLine();
waitlist[waitListCount] = name;

// here's the part I think you're having problems with
// if person is deleted from attendees
// get name of person not attending
int i = 0;
while (!cancelledName != attendees[i]) {
    i++
}

// replace attendees index with waitlist Name
attendees[i] = waitlist[0];

// move the waitlist forward
waitlist[0] = waitlist[1];
waitlist[1] = null;

就逻辑而言,这是我能想到的最好的。如果你的逻辑是合理的并且与我的相似,那么只关注底部,我认为你遇到的问题最多

答案 2 :(得分:0)

您无法将ArrayList与String进行比较,而是使用contains方法查找ArrayList中是否存在元素。

boolean isElementExists = myStringList.contains(canceledname);

if(isElementExists) {
   // other codes here
}

请参阅API:ArrayList#contains()