Get random strings from a list

时间:2016-02-12 22:10:11

标签: java arraylist random

I have an ArrayList which is defined outside the main method, just inside the class StringRandomize:

np.vstack((x[0:3,:],y[0:3,:]))

I also initialized a random object.

 public static ArrayList<String> countries = new ArrayList<String>();

Then I add some Strings to the list:

 Random obj = new Random();

How do I make those strings appear randomly? I need output like "Germany", "Moldova" and so on.
I need exactly the strings in the output, not their IDs. Thanks for your help.

3 个答案:

答案 0 :(得分:3)

You probably want to use something like:

hy=1
while(y==1):
passwordentered=str(input("plaese enter your proposed password "))
x=len(passwordentered)
numbers=passwordentered.count ("1"and"2"and"3"and"4"and"5"and"6"and"7"and"8"and"9")
lowerletters=passwordentered.count ("a"and"b"and"c"and"d"and"e"and"f"and"g"and"h"and"i"and"j"and"k"and"l"and"m"and"n"and"o"and"p"and"q"and"r"and"s"and"t"and"u"and"v"and"w"and"x"and"z")
higherletters=passwordentered.count ("A"and"B"and"C"and"D"and"E"and"F"and"G"and"H"and"I"and"J"and"K"and"L"and"M"and"N"and"O"and"P"and"Q"and"R"and"S"and"T"and"U"and"V"and"W"and"X"and"Z")
if(numbers>0 and lowerletters==0 and higherletters==0):
    david=9
elif(lowerletters>0 and numbers==0 and higherletters==0):
    david=9
elif(higherletters>0 and numbers==0 and lowerletters==0):
    david=9
elif(higherletters>0 and numbers>0 and lowerletters==0):
    david=8
elif(higherletters>0 and lowerletters>0 and numbers==0):
    david=8
elif(numbers>0 and lowerletters>0 and higherletters==0):
    david=8
elif(numbers>0 and lowerletters>0 and higherletters>0):
    david=7
elif(x>12 or x<6):
    david=10
elif(lowerletters==0 and numbers==0 and higherletters==0):
    david=10
if(david==10):
    print("the password you entered was invalid\
why not try again.")
    y=1
elif(david==9):
   print("the password you entered is very weak,try to include numbers, lower case letters and upper case letters. why not have another go.")
   y=1
elif(david==8):
    print("your password is good but it could be better try to include numbers, lower case letters and upper case letters. why not have another go.")
    y=1
elif(david==7):
    print("your password is really good. thank you for using this program")
    y=0

Or, to avoid creating a new countries.get(Math.abs(new Random().nextInt()) % countries.size()); object every time, you could use the same one:

Random

答案 1 :(得分:1)

date             count            
2015-11-05        2
2015-11-06        1

This way you shuffle the entire array and get the values in a random order but NO duplicate at all. Every single element changes position, so that no matter what element (position) you pick, you get a country from a random position. You can return the entire vector, the positions are random.

答案 2 :(得分:1)

如果你想要一个随机列表,我会使用Collections.shuffle(countries)

另外像弗拉维乌斯一样new Random().nextInt(max)描述。