0

I have a array-list which has data from my table. I want to create another array-list only containing integer from 0 to 23. The data has strings, negative numbers. It would be great if someone could give me example of it.

 int col = 2; 
 List values = new ArrayList(table.getRowCount());

 for (int row = 0; row < table.getRowCount(); row++) {
  values.add(table.getValueAt(row, col));
  }

4 Answers 4

1

You could do the following-
1. Check if it is a String. If it is, then try parsing the string and if it is successful and it is between 0 and 23, then add to a new list.
2. Check if it is an Integer. If it is, then check if it is between 0 and 23, then add to a new list.

List<Object> inputList; // List containing all kinds of objects
List<Integer> newList = new ArrayList<>();

for (Object o: inputList) {
     if (o instanceof String) {
        String s = (String) o;
        try {
            int n = Integer.parseInt(s)
            if (n >= 0 && n <= 23) {
               newList.add(n);
            }
        } catch (NumberFormatException e) {
         System.out.println(s + " is not an integer");
        }
     }
     else if (o instanceof Integer) {
        int n = (Integer)o;
        if (n >= 0 && n <= 23) {
           newList.add(n);
        }
     }
}
Sign up to request clarification or add additional context in comments.

10 Comments

it comes out empty
Can you give sample input? Does your data has integers represented as String? In that case, you might have to parse them
[T0W, O9N, S8Z, J2Q, X2YE, 8B, 8U, 3L, 7G, 0Z, 5A, 4A, 3V, 0M, 6C, 9U, 0V, 9I, 2E, 6S, 5M, 7V, 1R, 9F, 1B, 16, 22,-1, -22] it is represented as string
@hello88 I added Java7 solution to my answer, it must fit your input data.
instead of (String s: inputList) (Object s: inputList) and just add Sting to this int n = Integer.parseInt((String) s) . It's perfect for me guys! thank you so much to both of you!!
|
1

Given the original list is a list of strings List<String> originalList = new ArrayList<>(); the solution would be:

Java8

List<Integer> filteredList = originalList
            .stream()
            .filter(s -> s.matches("-?[0-9]+")) // filter only strings which contain numbers
            .map(Integer::parseInt) // convert them to integers
            .filter(v -> v >= 0 && v <= 23) // check if it fits the range
            .collect(Collectors.toList());

Java 7

for (String s: originalList) {
    if (s.matches("-?[0-9]+")) {
        int n = Integer.parseInt(s);
        if (n >= 0 && n <= 23) {
            filteredList.add(n);
        }
    }
}

Sample output:

original list: [-10, -9, str, str, str, -5, -4, str, str, -1, 0, 1, 2, str, str, str, str, 7, str, 9]

filtered list: [0, 1, 2, 7, 9]

Comments

0

So you just to copy few items from one list to another and I am assuming they are in sequence.

    //Consider you have this list already
    List<Object> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));

    //This is the new list you want to create
    List<Object> newList = new ArrayList<>();

    int i = 0, count = 5;
    for(Object o : list){
        if(i++ == count) {
            break;
        }
        newList.add(o);
    }
    System.out.println(newList);

Output: newList : [1, 2, 3, 4, 5]

1 Comment

they aren't in sequence
0
List<Object> tableValues = new ArrayList<>(Arrays.asList(
            new Long(100), new ArrayList(), 3, -6, 20, new Float(1.232), 4));
List<Integer> newValues = new ArrayList<>();

for (Object o : tableValues) {
    if (o instanceof Integer) {
        if ((Integer)o >= 0 && (Integer)o <= 23) {
            newValues.add((Integer)o);
        }
    }
}

System.out.println(newValues);

Output is [3, 20, 4]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.