Java ArrayList clone improved run-time - java

This is the question:https://leetcode.com/problems/combinations/
This is my solution 1:
public class Solution {
public List<List<Integer>> combine(int n, int k){
List<List<Integer>> result = new ArrayList<List<Integer>>();
combine(n, k, 1, result, new ArrayList<Integer>());
return result;
}
public void combine(int n, int k , int start, List<List<Integer>> result, ArrayList<Integer> l){
if(k == 0){
result.add(l);
return;
}
for(int i = start; i <= n; ++i){
l.add(i);
combine(n, k - 1, i + 1, result, l);
}
}
}
Result: small test cases passed. But big test cases time exceed.
Submission Result: Time Limit Exceeded
Last executed input: 10, 5
Solution 2:
public class Solution {
public List<List<Integer>> combine(int n, int k){
List<List<Integer>> result = new ArrayList<List<Integer>>();
combine(n, k, 1, result, new ArrayList<Integer>());
return result;
}
public void combine(int n, int k , int start, List<List<Integer>> result, ArrayList<Integer> l){
if(k == 0){
result.add(l);
return;
}
for(int i = start; i <= n; ++i){
ArrayList<Integer> a = (ArrayList<Integer>) l.clone();
a.add(i);
combine(n, k - 1, i + 1, result, a);
}
}
}
Passed all test cases.
the main difference is clone of the list.
But why?
is the solution A wrong or just slow?
Why using clone is faster here?
Really confused.

The first solution is indeed incorrect. Try invoking combine(5,3), and sending it to System.out, and you'll see the output for the first is:
[[1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5]]
You'll notice that it's the same list in each index position - you really do need to create a new array each time. For the second, correct solution, the output is:
[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
This means that the first solution is slower because you're adding numbers to a larger and larger list each time. For higher values of n and k, that list can be very large, and copying the backing array of ArrayList when it needs to expand becomes a very expensive operation - much more expensive than copying/creating a number of small lists.

Related

Not able to solve Plus One Java problem on Leetcode [duplicate]

This question already has answers here:
Plus one leetcode
(7 answers)
Closed 4 months ago.
I am trying to solve Plus One on Leetcode
The description is as follows:
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
My code is:
class Solution {
public int[] plusOne(int[] digits) {
long temp=0;int c=0;
for(int i:digits)
{
temp=temp*10+i;
//System.out.println(temp);
}
//System.out.println(temp);
temp+=1;
//System.out.println(temp);
long copy=temp;
while(copy>0)
{
copy/=10;
c++;
}
int[] result=new int[c];
for(int i=c-1;i>=0;i--)
{
//System.out.println((int)temp%10);
result[i]=(int)temp%10;
// System.out.println(result[i]);
temp/=10;
}
return result;
}
}
The problem is when I am trying to extract the number in the unit's digit it is givig 9 instead of 1.
Expected output:[9,8,7,6,5,4,3,2,1,1]
My Output:[9,8,7,6,5,4,3,2,1,9]
There is nothing really wrong with your code...it just needs a little tweak.
The problem you are having is within the for loop that converts the incremented number held in temp to an int[] Array. Casting the temp % 10 to an int as in result[i] = (int) temp % 10; is going to produce an undesirable result. temp is a long and therefore shouldn't be cast to int until is has finished its modulo calculation. So, the simple solution would be to wrap the equation within parentheses, for example:
result[i] = (int) (temp % 10);
That should solve the immediate problem. An even bigger problem is the fact that you are limited to a numerical value that can only fit into a long data type. An int[] Array can hold an extremely large number if you were to take each element of that array and sequentially combine the digits. A long data type for example can only hold an unsigned value of 9223372036854775807. As you can see, this would create an int[] Array of 19 elements and that's a small Array.
What if the Array contained 100 elements (digits)? That's a pretty big number but not necessarily out of the realm of use. This is why you have been advised (in comments) to utilize the java.math.BigInteger class. With BigInteger, you can work with any size of integer number which is basically what you really need. Here is how you could carry out the same task using BigInteger:
public int[] plusOne(int[] digits) {
java.math.BigInteger num = java.math.BigInteger.ZERO;
// Convert int[] Array to a BigInteger number...
for (int b : digits) {
num = num.multiply(java.math.BigInteger.valueOf(10))
.add(java.math.BigInteger.valueOf(b));
}
// Add 1 to the determined BigInteger number
num = num.add(java.math.BigInteger.valueOf(1));
// Convert BigInteger number back to an Array
digits = num.toString().chars().map(c -> c - '0').toArray();
return digits;
}
As an example, let's run this method against an int[] Array of 100 elements. That would be an integer number consisting of 100 digits. Now that's a large number:
/* Auto-create an int[] array containing 100 elements of random digits
0 to 9 (the first digit of the Array will never be a 0). */
int[] array = new int[100];
for (int i = 0; i < 100; i++) {
int n = new java.util.Random().nextInt(9);
// Make sure the first digit isn't a 0.
if (i == 0 && n == 0) {
i--;
continue;
}
array[i] = new java.util.Random().nextInt(9);
}
System.out.println("Original: -> " + Arrays.toString(array));
/* Cycle the plusOne() method 12 times and display the array
within each cycle... */
for (int i = 0; i < 12; i++) {
array = plusOne(array);
// Display The array:
System.out.println("Plus One: -> " + Arrays.toString(array));
}
The Console Window should display something like this. You will need to scroll to the end to see the incrementing (plus one):
Original: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 2, 6]
Plus One: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 2, 7]
Plus One: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3,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6, 6, 3, 5, 3, 8]
This is pretty straight forward solution. Just what you do in the school: increment right to left with keeping carry for the next digit.
class Solution {
public int[] plusOne(int[] digits) {
boolean carry = true;
for (int i = digits.length - 1; carry && i >= 0; i--) {
carry = digits[i] == 9;
digits[i] = carry ? 0 : digits[i] + 1;
}
if (carry) {
int[] tmp = new int[digits.length + 1];
tmp[0] = 1;
System.arraycopy(digits, 0, tmp, 1, digits.length);
digits = tmp;
}
return digits;
}
}

Java; saving array of arrays into collection

So I have this data
{ { 1, 3, 5, 3, 1 },
{ 3, 5, 6, 5, 1 },
{ 7, 2, 3, 5, 0 },
{ 12, 1, 5, 3, 0 },
{ 20, 6, 3, 6, 1 },
{ 20, 7, 4, 7, 1 } }
and i want to save it into some kind of collection, list or set. So if that collection was named List,if i were to type List[0][3] it would reffer to int 4.
I tried with
ArrayList<int[]> myNumberList = new ArrayList<int[]>();
but i have trouble putting that data into list
The array access operator [] is only applicable to arrays. So you can only create 2-dimensional array.
int a[][] = new int[][]{
{1, 3, 5, 3, 1},
{3, 5, 6, 5, 1},
{7, 2, 3, 5, 0},
{12, 1, 5, 3, 0},
{20, 6, 3, 6, 1},
{20, 7, 4, 7, 1}
};
System.out.println(a[0][3]);
But you can't create any type of collection that can use [] to access it's values.
Yoy can still use List of arrays. But you will have to index first dimension, using get() method
List<int[]> a2 = Arrays.asList(
new int[]{1, 3, 5, 3, 1},
new int[]{3, 5, 6, 5, 1},
new int[]{7, 2, 3, 5, 0},
new int[]{12, 1, 5, 3, 0},
new int[]{20, 6, 3, 6, 1},
new int[]{20, 7, 4, 7, 1}
);
System.out.println(a2.get(0)[3]);
You could make it an Integer[][] and create a List<List<Integer>>. Something like,
Integer[][] arr = { { 1, 3, 5, 3, 1 }, { 3, 5, 6, 5, 1 },
{ 7, 2, 3, 5, 0 }, { 12, 1, 5, 3, 0 }, { 20, 6, 3, 6, 1 },
{ 20, 7, 4, 7, 1 } };
System.out.println(Arrays.deepToString(arr));
List<List<Integer>> al = new ArrayList<>();
for (Integer[] inArr : arr) {
al.add(Arrays.asList(inArr));
}
System.out.println(al);
which outputs (formatted for this post)
[[1, 3, 5, 3, 1], [3, 5, 6, 5, 1], [7, 2, 3, 5, 0],
[12, 1, 5, 3, 0], [20, 6, 3, 6, 1], [20, 7, 4, 7, 1]]
[[1, 3, 5, 3, 1], [3, 5, 6, 5, 1], [7, 2, 3, 5, 0],
[12, 1, 5, 3, 0], [20, 6, 3, 6, 1], [20, 7, 4, 7, 1]]
Hard to answer what do you really need in your particular case. But in general list-equivalent of 2-dimensional array, which, I guess, you are looking for, will be List<List<Integer>> type, and in java-8 you can convert it in such way:
int a[][] = new int[][]{
{1, 3, 5, 3, 1},
{3, 5, 6, 5, 1},
{7, 2, 3, 5, 0},
{12, 1, 5, 3, 0},
{20, 6, 3, 6, 1},
{20, 7, 4, 7, 1}};
List<List<Integer>> l2 = new ArrayList<>();
Stream.of(a).forEach(a1 -> l2.add(Arrays.stream(a1).boxed().collect(Collectors.toList())));

why the list behaves differently in python and Java?

I am learning the scripting language, python. I know Java quite a bit. I am trying to translate one bit of code from Java to python. but they behave erratically (or my understanding could be totally wrong)
I have the following code in Java, where I am adding elements to a ArrayList indefinitely.
so this causes outofmemory error, which I expect:
import java.util.*;
public class Testing{
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(4);
for (int i=0;i<list.size();i++){
list.add(5);
}
}
}
now the same code translated in python:
lst = []
lst.append(5)
lst.append(4)
for i in range(len(lst)):
lst.append(5)
print lst
here I get the output: [5, 4, 5, 5]
from what I see, is the list not passed as a reference to the for loop in python?
similarly here,
>>> l=[1,2,3]
>>> for i in l[:]:
... l.append(4)
... print l
...
[1, 2, 3, 4]
[1, 2, 3, 4, 4]
[1, 2, 3, 4, 4, 4]
in each iteration inside the for loop, I am increasing the list size, so the iteration should go forever correct?
A python for loop evaluates the expression that yields the iterable over which to loop once only. You can manipulate the lst object in the loop without affecting the outcome of what for is looping over. This differs from the Java for construct (which is a very different construct from the Python for statement, which is really a Foreach construct), which evaluates the 3 associated expressions for each iteration.
In your first example, you created a range() result, and once that is created it is not updated for each loop iteration.
In your second example, you created a copy of lst using a full-length slice (lst[:]) for the loop to iterate over. The copy is not re-created for each loop iteration.
There is a caveat here, however. The for loop calls iter() on the object-to-be-iterated-over. For a list object, the resulting list iterator does keep a reference to the original list, as well as an iteration index. Every time the for loop advances the iterator (calling next() on it), the iteration index is incremented and looked up on the original list, until the index is equal to the list length. If you keep adding to the list in the loop, that would create an infinite loop.
You can see this if you do not create a copy of the list to iterate over:
>>> L = [1, 2, 3]
>>> for i in L:
... L.append(4)
... print L
... if len(L) > 30:
... break
...
[1, 2, 3, 4]
[1, 2, 3, 4, 4]
[1, 2, 3, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
Here, the iterator created for L keeps yielding the next element as L is extended in the loop, and if I had not added a length limit in the loop this would have continued forever.
range(len(lst)) creates the range once, and then iterates over that whereas in java list.size() is evaluated at each iteration
> is the list not passed as reference to the for-loop in python?
All objects are passed by reference in Python, and in Python everything is an object. (Java primitive values are not, but even plain integer and float values are objects in Python.)
> in each iteration inside for-loop, I am increasing the list size, so the iteration should go forever correct?
You are increasing the size of l, that's correct, but l[:] is evaluated just once and produces a shallow copy of l. That copy isn't changed in the loop, so changes to l inside the loop do not change the set of values that the loop variable will take on.
Change l[:] to just l in that loop, and then you'll see a lot of output.
It may be easiest to explain this by translating the for loops into their equivalent while loops.
In Java:
for (int i=0;i<list.size();i++){
list.add(5);
}
int i=0;
while (i<list.size()) {
list.add(5);
++i;
}
In Python pseudocode:
for i in range(len(lst)):
lst.append(5)
_r = range(len(lst))
_i = iter(r)
while _i isn't done:
next(_i)
lst.append(5)
In practice, you don't really need to understand how iter* and next work, or the details of how the "_i isn't done" part works**; the key is that a for loop creates an iterator, then iterates over it. In your case, it's creating an iterator over a range object (or, in Python 2.x, the list returned by the range function).
But even without knowing that, you can see that your len(lst) is only being evaluated once at the start, to create the iterator, while the Java list.size() equivalent is being evaluated each time through the loop.
If you want the equivalent of a Java-style for loop, you have to write the while loop explicitly.
* iter creates an iterator over any iterable (a list, a range, even another iterator). An iterator is sort of like a smart object that has a reference to the iterable and a "current position" within it, although under the covers they're rarely implemented that way. Calling next on an iterator effectively returns the value at the current position and advances the iterator to the next one (or the equivalent for however the iterator is actually implemented).
** What actually happens is, in effect, a try:/except StopIteration:, because calling next on an iterator that's done raises StopIteration. Of course it's implemented in C (or Java or .NET or RPython, for other Python implementations), and the C actually uses some special magic code for looping over an iterator that makes it a little faster, but almost nobody ever needs to think about that part.

Socket / Telnet-connection / BufferedReader

i have a multi-threaded notification-service which uses a simple protocol. When testing it with telnet it works fine, just one thing keeps bugging me: When i close the session by just closing the (OSX)Terminal window the BufferedReader.readLine reads some weird bytes, and doesn't throw an IOException.
This is the receiving code:
try {
/* wait for input from client */
InputStream in = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charsets.UTF_8));
while (true) {
try {
/* read a line */
String line = reader.readLine();
if (line == null)
break;
L.debug("length {} bytes {}",line.length(),line.getBytes());
// do stuff
} catch (final ParseException e) {
L.error("{} parsing failed: {}", ClientConnection.this, e.getMessage());
}
}
} catch (final IOException e) {
L.error("{} IOException: {}", ClientConnection.this, e.getMessage());
}
When i close the telnet-session the code reads a line, the length is arbitray (400 or 232 or 123), and bytes[] ares always a series of 4s, like this:
2012-02-06 01:51:19,575 [pool-1-thread-5] DEBUG - as [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 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, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
Actually 4 means End of Transmission, so that should be the expected behaviour for a telnet session. (I guess it will stop sending 4s as soon as the socket really is closed)
see ascii table here: http://www.robelle.com/smugbook/ascii.html
I'm not sure why there are so many but 4 is US ASCII End of Transmission

Java array to multi dimensional

Is it possible to take an array of say 100 chars and turn it into a 2d array of 10*10?
Here you go
char[] chars = ("01234567890123456789012345678901234567890123456789" +
"01234567890123456789012345678901234567890123456789")
.toCharArray();
char[][] char2D = new char[10][10];
for (int i = 0; i < 100; i++)
char2D[i / 10][i % 10] = chars[i];
Now the this code...
System.out.println(Arrays.deepToString(char2D).replaceAll("],","],\n"));
...prints the following
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
Iterate throughout your list of 100 chars and divide it amongst the 10*10,
Modulus (%) will probably be very useful.
You could use 2 nested for loops to assign the chars of the array to the appropriate element.

Categories