小编jl9*_*l90的帖子

使用空值对数组进行排序

我有一个程序,允许用户从数组中删除一个元素,我试图使用compareTo()按字母顺序对它们进行排序; 通过for循环.但是,空值给我带来了问题.例如,具有空值的数组:

String[] myArray = {"Apple", "Banana", null, "Durian", null, null, "Grapes"};
Run Code Online (Sandbox Code Playgroud)

当Java比较它们并读取空值时,它会给我一个NullPointerException.

有什么方法可以在后面用空值对这个数组进行排序吗?例如:

{"Apple", "Banana", "Durian", "Grapes", null, null, null}
Run Code Online (Sandbox Code Playgroud)

我知道使用Vectors可以解决问题,但我只是好奇,如果有任何方法我可以做到这一点,而无需将我的数组更改为向量.

java arrays

17
推荐指数
2
解决办法
2万
查看次数

nextLine()的问题;

可能重复:
在nextInt之后使用nextLine时出现扫描程序问题

我正在尝试创建一个程序,它允许用户使用扫描仪将值输入到数组中.

但是,当程序要求学生的近亲时,它不会让用户输入任何内容并立即结束该程序.

以下是我所做的代码:

if(index!=-1)
    {
        Function.print("Enter full name: ");
        stdName = input.nextLine();

        Function.print("Enter student no.: ");
        stdNo = input.nextLine();

        Function.print("Enter age: ");
        stdAge = input.nextInt();

        Function.print("Enter next of kin: ");
        stdKin = input.nextLine();

        Student newStd = new Student(stdName, stdNo, stdAge, stdKin);
        stdDetails[index] = newStd;
    }
Run Code Online (Sandbox Code Playgroud)

我尝试过使用next(); 但它只会取用户输入的第一个字而不是我想要的.反正有没有解决这个问题?

java java.util.scanner

11
推荐指数
2
解决办法
4万
查看次数

在JOptionPane中更新消息

我正在尝试制作一个在JOptionPane中显示时间的数字时钟.我已设法在消息对话框中显示时间.但是,我无法弄清楚如何让它在对话框中每秒更新一次.

这就是我目前拥有的:

    Date now = Calendar.getInstance().getTime();
    DateFormat time = new SimpleDateFormat("hh:mm:ss a.");

    String s = time.format(now);

    JLabel label = new JLabel(s, JLabel.CENTER);
    label.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 20));

    Toolkit.getDefaultToolkit().beep();

    int choice = JOptionPane.showConfirmDialog(null, label, "Alarm Clock", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
Run Code Online (Sandbox Code Playgroud)

java swing jlabel joptionpane

3
推荐指数
1
解决办法
3007
查看次数

仅当检查整个数组时才使用 Else 语句

我希望我的程序只打印一次“else 语句”,只有在 for 循环已经搜索了我的“orderProduct”数组长度之后。

我目前拥有的:

do
{
    System.out.print("Enter Order: "); 
    userOrder = in.next();

    for(int i=0; i<orderProduct.length; i++)
    {
        if(userOrder.equals(orderProduct[i].code))
        {
            System.out.print("Enter Amount: ");
            userQuantity = in.nextInt();
            break;
        }
        else
        {
            System.out.println("Sorry, invalid order.");
        }
    }
}
while(!userOrder.equals("0"));
Run Code Online (Sandbox Code Playgroud)

这段代码总是在检查数组的第一个元素后执行“else 语句”。只有当 for 循环遍历整个数组时,有没有办法打印它?

java loops if-statement

2
推荐指数
1
解决办法
2341
查看次数

确保数组中的所有元素都不为零

我怎样才能以更简单的方式编写代码?

for(int i=0; i<array.length; i++)
{
    if(myArray[i] == 0)
    {
        myBoolean = false;
        break;
    }
    else if(myArray[0] != 0 && myArray[1] != 0 && myArray[2] != 0)
    //may continue depending on array length
    {
        myBoolean = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在拥有的是"if else语句",如果在我的数组中,只是一个元素不为零,它会将我的布尔值更改为true.但我需要的是确保数组中的所有元素都不为零,然后将布尔值更改为true.

java arrays

0
推荐指数
1
解决办法
1323
查看次数

ListView将按钮推出屏幕

我试图让ListView保持相同的大小,即使它溢出.但是,一旦ListView溢出,它就会将屏幕下方的按钮按下.如何防止它推动其下方的其他元素?

从上面的图像中可以看出,第二个显示一旦ListView溢出,按钮就会被推离屏幕.

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/splash_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout 
    android:background="@color/splash_bg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:paddingBottom="10dp"
        android:textColor="@color/splash_text"
        android:text="@string/sub" />

    <EditText 
        android:id="@+id/grp_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/field1"
        android:lines="1" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/members"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textSize="15sp"
            android:paddingTop="5dp"
            android:textColor="@color/splash_text"
            android:text="@string/members" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/splash_bg"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/name"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="@string/field2" />

            <Button
                android:id="@+id/add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/add" />

        </LinearLayout>

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:background="@color/splash_bg" />

    </LinearLayout> …
Run Code Online (Sandbox Code Playgroud)

android android-listview

0
推荐指数
1
解决办法
1739
查看次数