英文:
From a linked list to an array (Function)
问题
以下是您要翻译的代码部分:
public class MainClass {
public static int[] makeListIntoArray(Node<Integer> n) {
int[] arr1 = new int[countSizeOfList(n)];
for (int i = 0; i < countSizeOfList(n); i++) {
arr1[i] = n.getValue();
n = n.getNext();
}
return arr1;
} //EndOfFunction
public static int[] finalFunction2(Node<Integer> n) {
if (n == null) return ifListIsEmpty(n);
return makeListIntoArray(n);
}
public static int[] ifListIsEmpty(Node<Integer> n) {
Node<Integer> n2 = new Node<Integer>(999);
int[] arr1 = new int[countSizeOfList(n2)];
int i = 0;
arr1[i] = n2.getValue();
return arr1;
}
public static void main(String[] args) {
Node<Integer> n1 = new Node<Integer>(5);
Node<Integer> n2 = new Node<Integer>(4);
Node<Integer> n3 = new Node<Integer>(3);
Node<Integer> n4 = new Node<Integer>(5);
Node<Integer> n5 = new Node<Integer>(1);
n1.setNext(n2);
n2.setNext(n3);
n3.setNext(n4);
n4.setNext(n5);
System.out.println(finalFunction2(n1));
} //Main
} //Class
希望这可以帮助您。如果您有任何其他问题,请随时提出。
英文:
I'm trying to give a function a list and make an array which contains the value of the nodes of the list (Not complicated). There it is:
public class MainClass {
public static int[] makeListIntoArray(Node<Integer> n) {
int[] arr1 = new int [countSizeOfList(n)];
for(int i = 0; i < countSizeOfList(n); i++) {
arr1[i] = n.getValue();
n = n.getNext();
}
return arr1;
}//EndOfFunction
public static int[] finalFunction2(Node<Integer> n) {
if(n == null) return ifListIsEmpty(n);
return makeListIntoArray(n);
}
public static int[] ifListIsEmpty(Node<Integer> n) {
Node<Integer> n2 = new Node<Integer>(999);
int[] arr1 = new int [countSizeOfList(n2)];
int i = 0;
arr1[i] = n2.getValue();
return arr1;
}
public static void main(String[] args) {
Node<Integer> n1 = new Node<Integer>(5);
Node<Integer> n2 = new Node<Integer>(4);
Node<Integer> n3 = new Node<Integer>(3);
Node<Integer> n4 = new Node<Integer>(5);
Node<Integer> n5 = new Node<Integer>(1);
n1.setNext(n2);
n2.setNext(n3);
n3.setNext(n4);
n4.setNext(n5);
System.out.println(finalFunction2(n1));
}//Main
}//Class
Thing is that it prints "[I@7960847b" beside of the actual array... fixes?
Any fixes?
答案1
得分: 1
如果您想将一个值插入到数组中,那么应该有一个特定的索引,特别是一个静态的索引。您不能像对原始类型一样简单地将其分配给arr1。
例如,arr1[0] = n.getValue() 是有效的,但arr1 = n.getValue() 不是有效的。
public static int[] makeListIntoArray(Node<Integer> n) {
int[] arr1 = new int[countSizeOfList(n)];
int idx = 0;
while (n != null) {
arr1[idx++] = n.getValue();
n = n.getNext();
}
return arr1;
}//EndOfFunction
英文:
If you want to insert a value into array then there should be an index especially a static one. You cannot simply assign it to arr1 like your do for primitive types.
For example, arr1[0] = n.getValue() is valid but not arr1 = n.getValue();
public static int[] makeListIntoArray(Node<Integer> n) {
int[] arr1 = new int [countSizeOfList(n)];
int idx=0;
while(n != null) {
arr1[idx++] = n.getValue();
n = n.getNext();
}
return arr1;
}//EndOfFunction
答案2
得分: 1
如果您正在使用Java的内置LinkedList数据结构,您可以简单地使用以下方法将LinkedList转换为数组:
Integer[] array = list.toArray(new Integer[list.size()]);
因此,对于您描述的情况,您所需要的函数如下所示:
import java.util.LinkedList;
public class MainClass {
public static int[] makeListIntoArray(LinkedList<Integer> list) {
Integer[] arr = list.toArray(new Integer[list.size()]);
int[] intArr = Arrays.stream(arr).mapToInt(Integer::intValue).toArray();
// 上面的代码将包装类Integer[]转换为int[],如果您需要的话
return intArr;
}
}
您可以在这里找到有关LinkedList的toArray()
方法的更多信息。
英文:
If you're using Java's built-in LinkedList data structure you can simply use the following to convert from LinkedList to array:
Integer[] array = list.toArray(new Integer[list.size()]);
So for the situation you're describing all you would need for the function is:
import java.util.LinkedList;
public class MainClass {
public static int[] makeListIntoArray(LinkedList<Integer> list) {
Integer[] arr = list.toArray(new Integer[list.size()]);
int[] intArr = Arrays.stream(array).mapToInt(Integer::intValue).toArray();
// Above line converts the wrapper Integer[] to int[] if you need that
return intArr;
}
}
You can find more info about LinkedList's toArray()
method here.
答案3
得分: 0
如果您想保留链表但还想以O(1)的时间访问元素,您可以创建一个Node数组。
public class MainClass {
public static Node<Integer>[] makeListIntoArray(Node<Integer> n) {
Node<Integer>[] arr1 = new Node<Integer>[countSizeOfList(n)];
int i=0;
while(n != null) {
arr1[i] = n;
n = n.getNext();
++i;
}
return arr1;
}//EndOfFunction
}//Class
英文:
If you want to keep the linked list but also want to access the elements in O(1), you can create an array of Nodes.
public class MainClass {
public static Node<Integer>[] makeListIntoArray(Node<Integer> n) {
Node<Integer>[] arr1 = new Node<Integer> [countSizeOfList(n)];
int i=0;
while(n != null) {
arr1[i] = n;
n = n.getNext();
++i;
}
return arr1;
}//EndOfFunction
}//Class
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论