小编Kev*_*sen的帖子

强制 GSON 使用包含 Setters 或一般 Setters 的构造函数,而不是 Fields

假设我有以下模型类:

public class Product
{
    private int ProductId;
    private String Name;

    public Product(){
        setProductId(0);
        setName("");
    }

    // Getter and Setter for the Product-ID
    public void setProductId(int i){
        if(i >= 0) {
            ProductId = i;
        } else {
            ProductId = 0;
        }
    }
    public int getProductId(){
        return ProductId;
    }

    // Getter and Setter for the Name
    public void setName(String n){
        if(n != null && n.length() > 0) {
            name = n;
        } else {
            name = ""; 
        }
    }
    public String …
Run Code Online (Sandbox Code Playgroud)

java android json converters gson

5
推荐指数
1
解决办法
3374
查看次数

Java - ListIterator的无限循环.hasNext()

ArrayList<String> list = new ArrayList<String>();
list.add("test");

while(list.listIterator().hasNext()) {
    System.out.println(list.listIterator().next());
}
Run Code Online (Sandbox Code Playgroud)

这会产生一个带有"测试"的无限循环线.为什么会发生这种情况以及如何解决?

java arraylist infinite-loop listiterator

5
推荐指数
1
解决办法
6147
查看次数

日历给出了第1年的意外结果

我试图在Java 7中进行代码 - 高尔夫挑战.只是对于任何不知道的人:是以尽可能少的字节完成某个任务.显然,Java不是一种合适的编程语言,尤其是像Jelly这样的语言; 05AB1E; Pyth; 同样以1-15个字节完成任务,在Java中为75-300,但我只是为了好玩.

这是我目前的Java 7答案.仅供参考,我也会在这里复制:

import java.util.*;String c(int y){String r="";Calendar c=Calendar.getInstance();c.set(1,y);c.set(2,0);for(int i=0;i++<11;c.add(2,1)){c.set(5,c.getActualMaximum(5));if(c.get(7)==2)r+=i+" ";}return r;}
Run Code Online (Sandbox Code Playgroud)
import java.util.*;
class M{
  static String c(int year){
    String r = "";
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, 0);
    for(int i = 0; i++ < 11; calendar.add(Calendar.MONTH, 1)){
      calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
      if(calendar.get(Calendar.DAY_OF_WEEK) == 2){
        r += i+" ";
      }
    }
    return r;
  }

  public static void main(String[] a){
    System.out.println(c(1));
    System.out.println(c(297));
    System.out.println(c(1776));
    System.out.println(c(2000));
    System.out.println(c(2016));
    System.out.println(c(3385));
  }
}
Run Code Online (Sandbox Code Playgroud)

哪个输出所有1个索引月份,其中月份的最后一天是星期一: …

java calendar date dayofweek

5
推荐指数
1
解决办法
115
查看次数

Vaadin 8 Converter 的行为与 Vaadin 7 Converter 不同(不更新 UI)?

TL;DR:Vaadin 8 中是否有类似于 Vaadin 7 的转换器来更新 UI 中输入字段的表示?即在输入字段失去焦点后立即从用户输入中删除所有非数字,或将小数转换为货币?

Vaadin 论坛链接:http : //vaadin.com/forum#!/ thread/ 15931682


我们目前正在将 Vaadin 7 项目迁移到 vaadin 8。

在我们的 Vaadin 7 应用程序中,我们有几个转换器。例如这个 CurrencyConverter:

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import org.apache.commons.lang3.StringUtils;
import com.vaadin.v7.data.util.converter.StringToBigDecimalConverter;
import com.vaadin.v7.data.util.converter.Converter.ConversionException;

/**
 * A converter that adds/removes the euro sign and
 * formats currencies with two decimal places.
 */
public class EuroConverter extends StringToBigDecimalConverter {

    @Override
    public BigDecimal convertToModel(String value,
                                     Class<? extends BigDecimal> targetType,
                                     Locale locale) throws ConversionException {
        if(StringUtils.isBlank(value)) { …
Run Code Online (Sandbox Code Playgroud)

java converter vaadin vaadin7 vaadin8

5
推荐指数
1
解决办法
1534
查看次数

重现行为 MAX_VALUE 和 MIN_VALUE

以下内容也适用于其他MIN_VALUE和,但我们现在MAX_VALUE只关注。Integer我知道在Java中整数是32位的,有Integer.MAX_VALUE = 2147483647(2 31 -1)和Integer.MIN_VALUE = -2147483648(-2 31 )。当使用这些值进行计算时,当超出其范围时,数字会环绕/溢出。因此,当您执行类似操作时Integer.MAX_VALUE + 1,结果与 相同Integer.MIN_VALUE

MIN_VALUE以下是使用和 进行的一些基本算术计算MAX_VALUE

Integer.MAX_VALUE:                      2147483647
Integer.MAX_VALUE + 1:                  -2147483648
Integer.MAX_VALUE - 1:                  2147483646
Integer.MAX_VALUE * 2:                  -2
Integer.MAX_VALUE * 3:                  2147483645
Integer.MAX_VALUE * 4:                  -4
Integer.MAX_VALUE * 5:                  2147483643
Integer.MAX_VALUE / Integer.MAX_VALUE:  1
Integer.MAX_VALUE * Integer.MAX_VALUE:  1
Integer.MAX_VALUE / Integer.MIN_VALUE:  0
Integer.MAX_VALUE * Integer.MIN_VALUE:  -2147483648
Integer.MAX_VALUE - Integer.MIN_VALUE:  -1
Integer.MAX_VALUE + Integer.MIN_VALUE: …
Run Code Online (Sandbox Code Playgroud)

java integer integer-overflow max min

5
推荐指数
1
解决办法
230
查看次数

Android - 使用自定义ArrayAdapter获取ListView的每个EditTexts值

我有一个带有客户ArrayAdapter和自定义ListItem-XML的ListView.在这个ListView的底部,我添加了一个带有Button的FooterView.现在,我想在单击此Button时获取每个ListItem中三个EditTexts的所有值.

以下是我的代码:

activity_checklistresult.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

result_inner_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <TextView
        android:id="@+id/tv_result_amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/default_margin" />
    <EditText
        android:id="@+id/et_result_amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/default_margin"
        android:inputType="number" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_result_product_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:layout_margin="@dimen/default_margin" />
        <AutoCompleteTextView
            android:id="@+id/actv_search_product"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:singleLine="true"
            android:ellipsize="end"
            android:layout_margin="@dimen/default_margin"
            android:inputType="textAutoComplete" />

    </LinearLayout>

    <TextView
        android:id="@+id/tv_result_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/default_margin" />
    <EditText
        android:id="@+id/et_result_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/default_margin"
        android:inputType="numberDecimal" />

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

ResultListActivity.java:

public …
Run Code Online (Sandbox Code Playgroud)

android list android-arrayadapter android-listview android-edittext

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

Android不支持Java v7 +,那么我应该使用多次捕获还是使用instanceof检查?

我有以下情况:

try{
    // Do some things that can cause the exceptions
}
catch(SomeException ex){
    doSomething();
}
catch(SomeOtherException ex){
    doSomething();
}
catch(AndYetAnotherException ex){
    doSomething();
}
catch(Exception ex){
    // Do something else
}
Run Code Online (Sandbox Code Playgroud)

在Java v7 +中,我可以将其更改为:

try{
    // Do some things that can cause the exceptions
}
catch(SomeException | SomeOtherException | AndYetAnotherException ex){
    doSomething();
}
catch(Exception ex){
    // Do something else
}
Run Code Online (Sandbox Code Playgroud)

由于Android尚不支持Java 7+,我无法使用上述内容.相反,执行以下操作有哪些风险:

try{
    // Do some things that can cause the exceptions
}
catch(Exception ex){
    if(ex instanceof SomeException || ex …
Run Code Online (Sandbox Code Playgroud)

java android exception try-catch java-7

4
推荐指数
1
解决办法
3812
查看次数

替换ArrayAdapter列表

我有一个自定义ArrayAdapter,它使用ArrayList<OrderedProductItem>我的ListView.在这个ListView中,我有一个AutoCompleteTextView,它也使用了一个自定义ArrayAdapter ArrayList<Product>.在初始化时,此AutoCompleteTextView包含所有产品的名称(大约1250),减1(OrderedProductItem的产品).

然后,用户可以使用Spinner选择Product-Tag并创建过滤ArrayList<Product>,因此我只获得包含此Product-Tag的所有产品的列表(仍然减去1).所以,我的问题是:替换ArrayAdapter对象列表的最佳方法什么?

我知道我可以在ListActivity中重新创建并重新分配一个新的ArrayAdapter实例:

myAdapter = new MyAdapter(this, R.layout.item_view, myList);
setListAdapter(myAdapter);
Run Code Online (Sandbox Code Playgroud)

我也知道我可以使用clear然后使用addAll替换所有内容:

// NOTE: myAdapter.setNotifyOnChange is kept on default (true)
myAdapter.clear();
myAdapter.addAll(myList);
Run Code Online (Sandbox Code Playgroud)

我想第二个可能是最好的,因为我们不想每次都创建一个新实例,或者ArrayAdapter是否有类似的方法setArray(),我只是在寻找时错过了它?或者是否有另一种方式比第二种选择更好的性能?

java android arraylist android-arrayadapter notifydatasetchanged

4
推荐指数
1
解决办法
4442
查看次数

Java 多维数组 - 为什么只定义第一个大小就足够了?

使用以下 Java 示例:

int[] array1 = new int[]; // Incorrect, since no size is given
int[] array2 = new int[2]; // Correct
int[][][] array3 = new int[][][]; // Incorrect, since no size is given
int[][][] array4 = new int[2][2][2]; // Correct
int[][][] array5 = new int[2][][]; // Correct (why is this correct?)
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是,为什么仅分配多维数组的第一个大小就足够了?我认为你总是必须分配一个大小,甚至是为多维数组的每个单独的数组部分分配一个大小,但今天我发现这array5对于 Java 来说也是一种正确的方法。现在我只是想知道为什么。有人可以举一些例子来说明为什么这适用于多维数组和/或其背后的推理吗?

另外,我想以下内容也适用:

int[][][] array6 = new int[][2][]; // Incorrect
int[][][] array7 = new int[][][2]; // Incorrect
int[][][] array8 = new int[][2][2]; // Incorrect
int[][][] …
Run Code Online (Sandbox Code Playgroud)

java arrays instantiation multidimensional-array

4
推荐指数
1
解决办法
246
查看次数

Java 8 currying函数,无法确定int []返回类型

假设我们有一个像这样的lambda函数:

Function<ArrayList<Integer>, int[]> func1 = a->new int[2];
Run Code Online (Sandbox Code Playgroud)

它的作用并不重要.重要的是:输入是一个ArrayList<Integer>,输出是一个int[].

使用一些基本的测试编译并运行没有问题:

int[] func1Result1 = func1.apply(new ArrayList<Integer>()); // Non-currying works with <Integer>
System.out.println(func1Result1);
System.out.println(func1Result1.getClass());
System.out.println(Arrays.toString(func1Result1));
System.out.println();

int[] func1Result2 = func1.apply(new ArrayList<>());        // Non-currying works with <>
System.out.println(func1Result2);
System.out.println(func1Result2.getClass());
System.out.println(Arrays.toString(func1Result2));
System.out.println();

int[] func1Result3 = func1.apply(new ArrayList());          // Non-currying works without <>
System.out.println(func1Result3);
System.out.println(func1Result3.getClass());
System.out.println(Arrays.toString(func1Result3));
System.out.println();
Run Code Online (Sandbox Code Playgroud)

在线尝试.


现在让我们假设我们有一个像这样的currying lambda函数:

Function<Object, Function<Object, int[]>> func2 = a->b->new int[2];
Run Code Online (Sandbox Code Playgroud)

同样,它的作用并不重要.这次currying函数有两个Object参数,仍然输出一个int[].

使用相同的基本测试再次编译和运行没有问题:

int[] func2Result1 = func2.apply(new ArrayList<Integer>()).apply(null); // Currying works with …
Run Code Online (Sandbox Code Playgroud)

lambda types currying java-8 java-10

4
推荐指数
1
解决办法
184
查看次数