我想生成此范围内 (0-255) 的 255 个唯一随机数。这样数组就不会包含重复的记录
short [] array =new short[255];
Random rand = new Random();
boolean flag=false;
for (int i=0;i<array.length;i++){
int random_integer = rand.nextInt(255-0) + 0;
for (int j=0;j<i;j++){
if ((short)random_integer==array[j]){
flag=true;
}
}
if (flag==false){
array[i]=(short)random_integer;
}
}
for (int i=0;i<array.length;i++){
System.out.println(array[i]);
}
Run Code Online (Sandbox Code Playgroud)
但我只得到前 20 0r 30 个具有值的项目,其余的数组项目等于零。
我创建了一个ArrayList大约30 000个值,经过排序后,我有很多连续的值,例如:
22001 22002 22003 22004 22010 22011 22020
我希望:22001 22004 22010 22011 22020
ArrayList<Long> l = new ArrayList<Long>(); // with my values
Collections.sort(l);
ArrayList<Long> liste = new ArrayList<Long>();
Run Code Online (Sandbox Code Playgroud) 我有字符串:
2 | HOME ELECTRONICS | | 0 | 0 | | | | | 0 | |
我想分隔|上面字符串中分隔的所有标记.
我试图用StringTokenizer它来标记它,但它不考虑空格作为标记.
此外,我尝试过split("|")但它将上述字符串的每个字符作为返回字符串数组中的元素.
我该怎么办?
我写了这段代码是为了显示拥有最高工资的员工的名字,但是当输出不正确时,它显示为null而不是"mmm kkk"!! 虽然我填写了表格,但这是内容:
这是我的代码,任何人都可以帮助我?:(
public static void displayMaxSalary() throws ClassNotFoundException, SQLException
{
int size=0;
int count=0;
String maxSalary=null;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/task4DB?"
+ "user=root&password=123");
PreparedStatement st = con.prepareStatement("select * from task4Table ");
ResultSet r1=st.executeQuery();
while (r1.next()) {
size++;
}
int salaries[]=new int[size];
String Names[]=new String[size];
while (r1.next()) {
salaries[count]= r1.getInt("salary");
Names[count]= r1.getString("fName")+""+r1.getString("lName");
count++;
}
for(int i=1;i< salaries.length;i++)
{
if(salaries[i]>salaries[i-1])
{
maxSalary= Names[i];
}
}
System.out.println("The name of employee who has the higher salary is :");
System.out.println( maxSalary);
} …Run Code Online (Sandbox Code Playgroud) 以下代码不起作用.任何人都可以告诉我以下代码有什么问题.逻辑上它应该工作......
package assignments;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IsPalindrome {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a Word:");
StringBuffer sb1 = new StringBuffer(br.readLine());
StringBuffer sb2 = new StringBuffer(sb1);
sb1.reverse();
if(sb2.equals(sb1))
System.out.println("Palindrome");
else
System.out.println("Not a Palindrome");
}
}
Run Code Online (Sandbox Code Playgroud)